Return Value From Array Using Value of Another Key

The following function can be used to return value from an array by giving another key and value.

/**
 * search for the country code from search results
 * @param {*} array 
 * @param {*} value 
 * @param {*} key 
 */
function filter(array, value, key) {
    return array.filter(key ? function (a) {
        return a[key] === value;
    }
        : function (a) {
            return Object.keys(a).some(function (k) {
                return a[k] === value;
            });
        });
}

Leave a comment

Your email address will not be published. Required fields are marked *