Created
August 18, 2020 00:40
-
-
Save tapmodo/24aa03aa34426ef467f74d99543c8775 to your computer and use it in GitHub Desktop.
Organize a flat array of objects into a better structure (~50% smaller)
This file contains 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
[ | |
{ | |
"brand": { | |
"name": "Antonio's Pizzeria", | |
"slug": "1532df73-c60f-4cca-b03d-f535e031a7e1" | |
}, | |
"location": { | |
"id": 1, | |
"name": "Downtown" | |
}, | |
"menu": { | |
"slug": "7851b71a-4c88-4fb8-9546-8ec9537397ab", | |
"name": "Lunch Menu" | |
} | |
}, | |
{ | |
"brand": { | |
"name": "Antonio's Pizzeria", | |
"slug": "1532df73-c60f-4cca-b03d-f535e031a7e1" | |
}, | |
"location": { | |
"id": 1, | |
"name": "Downtown" | |
}, | |
"menu": { | |
"slug": "a4bede2e-be21-4e30-8263-874e1e77dde9", | |
"name": "Breakfast Menu" | |
} | |
}, | |
{ | |
"brand": { | |
"name": "Antonio's Pizzeria", | |
"slug": "1532df73-c60f-4cca-b03d-f535e031a7e1" | |
}, | |
"location": { | |
"id": 2, | |
"name": "Valley" | |
}, | |
"menu": { | |
"slug": "d93473ad-e8ab-4461-892e-6593d29e598f", | |
"name": "Breakfast Menu" | |
} | |
}, | |
{ | |
"brand": { | |
"name": "Antonio's Pizzeria", | |
"slug": "1532df73-c60f-4cca-b03d-f535e031a7e1" | |
}, | |
"location": { | |
"id": 2, | |
"name": "Valley" | |
}, | |
"menu": { | |
"slug": "78960d5d-823a-4501-bdd2-86522f1681f3", | |
"name": "Dinner Menu" | |
} | |
}, | |
{ | |
"brand": { | |
"name": "Antonio's Pizzeria", | |
"slug": "1532df73-c60f-4cca-b03d-f535e031a7e1" | |
}, | |
"location": { | |
"id": 2, | |
"name": "Valley" | |
}, | |
"menu": { | |
"slug": "a7fb8a9d-8036-497e-9a7a-1e1e3daeaf88", | |
"name": "Lunch Menu" | |
} | |
} | |
] |
This file contains 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 _ = require('lodash') | |
function uniqueLocations (data) { | |
// First get a list of unique brands | |
const out = _.uniqBy(data.map(r => r.brand), r => r.slug) | |
// Then add on the locations and menus | |
out.forEach(brand => { | |
// Find unique matching locations for each brand | |
const mloc = _.uniqBy(data | |
.filter(r => r.brand.slug === brand.slug) | |
.map(r => r.location), r => r.id) | |
// Attach menus for each location | |
mloc.forEach(loc => { | |
const menus = _.uniqBy(data.filter(r => | |
r.brand.slug === brand.slug && | |
r.location.id === loc.id | |
).map(r => r.menu), r => r.slug) | |
loc.menus = menus | |
}) | |
brand.locations = mloc | |
}) | |
return out | |
} |
This file contains 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
[ | |
{ | |
"name": "Antonio's Pizzeria", | |
"slug": "1532df73-c60f-4cca-b03d-f535e031a7e1", | |
"locations": [ | |
{ | |
"id": 1, | |
"name": "Downtown", | |
"menus": [ | |
{ | |
"slug": "7851b71a-4c88-4fb8-9546-8ec9537397ab", | |
"name": "Lunch Menu" | |
}, | |
{ | |
"slug": "a4bede2e-be21-4e30-8263-874e1e77dde9", | |
"name": "Breakfast Menu" | |
} | |
] | |
}, | |
{ | |
"id": 2, | |
"name": "Valley", | |
"menus": [ | |
{ | |
"slug": "d93473ad-e8ab-4461-892e-6593d29e598f", | |
"name": "Breakfast Menu" | |
}, | |
{ | |
"slug": "78960d5d-823a-4501-bdd2-86522f1681f3", | |
"name": "Dinner Menu" | |
}, | |
{ | |
"slug": "a7fb8a9d-8036-497e-9a7a-1e1e3daeaf88", | |
"name": "Lunch Menu" | |
} | |
] | |
} | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment