Last active
February 3, 2016 17:47
-
-
Save ubergoober/d0ece002063088d898de to your computer and use it in GitHub Desktop.
Check if value for a key exists in an array of objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arrayOfObjects = [{key1: 'foo1', key2: 'bar1'}, {key1: 'foo2', key2: 'bar2'}]; | |
var keyNameToCheck = "key1"; | |
var valueToFind = 'foo1'; | |
function doesArrayOfObjectsContainValueForKey(array, keyName, value){ | |
var res = array.filter(function(obj){ | |
return obj[keyName] === value; | |
}); | |
return res.length === 0 ? false : true; | |
} | |
//usage | |
doesArrayOfObjectsContainValueForKey(arrayOfObjects, keyNameToCheck, valueToFind); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment