Last active
October 13, 2021 21:47
-
-
Save hunterwilhelm/580151498881c4c7c4344f59f09a1254 to your computer and use it in GitHub Desktop.
Convert a list of objects to a dictionary by one of their properties
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
/** | |
* Convert a list of objects to a dictionary by one of their properties | |
* Assumes that all objects have the property and the value is unique | |
* | |
* @param {Array<Object>} objects - The array of objects | |
* @param {string} property - The property to turn the list of objects into Dict | |
* @return {Object<Object>} Dictionary of unmodified objects with the value | |
* of the properties as keys | |
*/ | |
function objectsToDictByProperty(objects, property) { | |
return objects | |
.filter(o => o && o[property] != null) | |
.reduce((acc, o) => (acc[o[property]] = o, acc), {}); | |
} |
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
const orders = [ | |
{ | |
"id": "4VzzEviJGYUtAeSsJlI9QB", | |
"flavor": "Chocolate", | |
"pounds": 124, | |
"destination": "L1", | |
"date": "Oct 11 2021 11:52:41" | |
}, | |
{ | |
"id": "$h%yzMv;0#6euoX8+V&J", | |
"flavor": "Strawberry", | |
"pounds": 10, | |
"destination": "L2", | |
"date": "Oct 3 2021 13:32:11" | |
}, | |
{ | |
"id": "nn,#W3^nP#LQ&Amz02B3", | |
"flavor": "Bubble Gum", | |
"pounds": 10, | |
"destination": "L3", | |
"date": "Oct 23 2021 21:12:35" | |
}, | |
{ | |
"id": "7&JZK3,2Hi#8+hU?W*A0", | |
"flavor": "Cookie Dough", | |
"pounds": 32, | |
"destination": "L1", | |
"date": "Oct 23 2021 09:22:53" | |
} | |
]; | |
const ordersDict = objectsToDictByProperty(orders, "id"); | |
console.log(ordersDict); | |
// output: | |
// { | |
// "4VzzEviJGYUtAeSsJlI9QB": { | |
// "id": "4VzzEviJGYUtAeSsJlI9QB", | |
// "flavor": "Chocolate", | |
// "pounds": 124, | |
// "destination": "L1", | |
// "date": "Oct 11 2021 11:52:41" | |
// }, | |
// "$h%yzMv;0#6euoX8+V&J": { | |
// "id": "$h%yzMv;0#6euoX8+V&J", | |
// "flavor": "Strawberry", | |
// "pounds": 10, | |
// "destination": "L2", | |
// "date": "Oct 3 2021 13:32:11" | |
// }, | |
// "nn,#W3^nP#LQ&Amz02B3": { | |
// "id": "nn,#W3^nP#LQ&Amz02B3", | |
// "flavor": "Bubble Gum", | |
// "pounds": 10, | |
// "destination": "L3", | |
// "date": "Oct 23 2021 21:12:35" | |
// }, | |
// "7&JZK3,2Hi#8+hU?W*A0": { | |
// "id": "7&JZK3,2Hi#8+hU?W*A0", | |
// "flavor": "Cookie Dough", | |
// "pounds": 32, | |
// "destination": "L1", | |
// "date": "Oct 23 2021 09:22:53" | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Object list to dictionary.
Convert any list of objects to a dictionary. The only requirement is the specified property must be unique.