Skip to content

Instantly share code, notes, and snippets.

@ZeroPie
Last active July 19, 2020 10:14
Show Gist options
  • Save ZeroPie/fecfe86c529e7c8afb09f19f370f14ed to your computer and use it in GitHub Desktop.
Save ZeroPie/fecfe86c529e7c8afb09f19f370f14ed to your computer and use it in GitHub Desktop.
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