Created
January 12, 2016 22:53
-
-
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
This file contains hidden or 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
| /** | |
| * 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