Spread Syntax For Nested Javascript Object With Dynamic Properties For Grouping
const data = [{year:2019,month:1,id:'xd1'}, {year:2019,month:1,id:'xd2'}, {year:2019,month:1,id:'xd4'}, {year:2019,month:2,id:'xd1'}, {year:2018,month:1,id:'rd3'}, {year:2018,
Solution 1:
Just add some sh*t and sticks
:
const data = [{year:2019,month:1,id:"xd1"},
{year:2019,month:1,id:"xd2"},
{year:2019,month:1,id:"xd4"},
{year:2019,month:2,id:"xd1"},
{year:2018,month:1,id:"rd3"},
{year:2018,month:2,id:"rd6"},
{year:2018,month:2,id:"rd7"}
]
const result = data.reduce((state, d) => {
return {
...state,
[d.year]: {
...state[d.year],
[d.month]: [
...((state[d.year]||{})[d.month]||[]),
d.id]
}
}
},{})
console.log(result);
Post a Comment for "Spread Syntax For Nested Javascript Object With Dynamic Properties For Grouping"