Skip to content

Instantly share code, notes, and snippets.

@nickhsu
Created October 27, 2015 15:00
Show Gist options
  • Save nickhsu/e4debbdc758828272159 to your computer and use it in GitHub Desktop.
Save nickhsu/e4debbdc758828272159 to your computer and use it in GitHub Desktop.
a = [
{_id: "123", val: "123"},
{_name: "456", val: "456"}
];
result = a.reduce(function(previousValue, currentValue) {
if (currentValue._id) {
previousValue[currentValue._id] = currentValue;
}
if (currentValue._name) {
previousValue[currentValue._name] = currentValue;
}
return previousValue;
}, {});
console.log("a: %j", result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment