Last active
July 19, 2020 10:14
-
-
Save ZeroPie/fecfe86c529e7c8afb09f19f370f14ed to your computer and use it in GitHub Desktop.
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 items = [ | |
{ | |
category: "screwdrivers", | |
name: "screw1" | |
}, | |
{ | |
category: "perforators", | |
name: "perf1" | |
}, | |
{ | |
category: "perforators", | |
name: "perf2" | |
}, | |
{ | |
category: "screwdrivers", | |
name: "screw2", | |
}, | |
{ | |
category: "screwdrivers", | |
name: "screw3", | |
} | |
] | |
const myCats = items.reduce((acc, item) => { | |
acc[item.category] = { ammount: 0} | |
return acc | |
}, {}) | |
const categories = items.reduce((acc, ele) => { | |
if(ele.category === Object.keys(acc).find(key => key === ele.category)) { | |
acc[ele.category].ammount += 1 | |
} | |
return acc | |
}, myCats) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment