Skip to content

Instantly share code, notes, and snippets.

@KryptikOne
Created January 12, 2016 22:53
Show Gist options
  • Select an option

  • Save KryptikOne/585fa7fc35a81c19f3eb to your computer and use it in GitHub Desktop.

Select an option

Save KryptikOne/585fa7fc35a81c19f3eb to your computer and use it in GitHub Desktop.
Get the index of an object in an array, based on the value of a certain key
/**
* Function to Search an Array of Objects
* @param arr --- What array you're searching in
* @param key --- What key you should be looking at in each object
* @param val --- What value you're looking for
* @returns {*} - Returns you the found object that has the matching key or null if not found
*/
function getArrIndexByKeyValue(arr, key, val) {
for (var i = 0; i < arr.length; i++) {
if (arr[i][key] == val) {
return i;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment