Created
July 2, 2015 14:53
-
-
Save aurbano/089007f2bb271290352f to your computer and use it in GitHub Desktop.
Given an array of objects that have an Id, return an array where the Id is the 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
/** | |
* Given an array of objects that have an Id, return an array where the Id is the key | |
* @param arr Array to normalize | |
* @param key [Optional] Object key to use, defaults to Id | |
* @returns {Array} Normalized array | |
*/ | |
function normalizeArrayId(arr, key){ | |
var total = arr.length, | |
index = key || 'Id', | |
ret = []; | |
for(var i=0; i<total; i++){ | |
ret[arr[i][index]] = arr[i]; | |
} | |
return ret; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment