Skip to content

Instantly share code, notes, and snippets.

@aurbano
Created July 2, 2015 14:53
Show Gist options
  • Save aurbano/089007f2bb271290352f to your computer and use it in GitHub Desktop.
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
/**
* 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