-1
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);



const result = data.reduce((state,d)=>{
    return {
        ...state,
        [d.year]:{
            ...state[d.year],
            [d.month]:[].concat([state[d.year][d.month]],[d.id])
        }
    }
},{})

both return an Error TypeError: Cannot read property '1' of undefined How can I use the spread syntax to get a grouped result like this.

{'2019':{
   '1':["xd1","xd2","xd3"],
   '2':["xd1"]},
 '2018':{
   '1':["rd3"],
   '2':["rd6","rd7"]
 }

} 

Please consider using reduce and spread syntax and not chaining other methods because the its actually part of a bigger construct and other things wont help. Thx. EDIT: like stated in the comments. I would explicitly like solve it inline with spread operator that returns new objects or arrays. No extra libraries like lodash.

silverfighter
  • 6,762
  • 10
  • 46
  • 73
  • Possible duplicate of [Group array of object nesting some of the keys with specific names](https://stackoverflow.com/questions/48425797/group-array-of-object-nesting-some-of-the-keys-with-specific-names) – Heretic Monkey Feb 07 '19 at 22:02
  • When the `state` is empty, there won't be a `state[d.year]` so `state[d.year][d.month]` will be the error you see. – Pointy Feb 07 '19 at 22:03
  • @HereticMonkey I don't think this is a duplicate question. The object structure is slightly different and solutions contain lodash and not spread. – silverfighter Feb 07 '19 at 22:19
  • @Pointy yeah kind of know that is is the problem. But do not know how to fix it inline spread-style... this is basically the question I am asking. If you could provide a solution this would be great. – silverfighter Feb 07 '19 at 22:20
  • I am expecting that you can figure out the minor details needed to adapt the answer to your structure. There are several solutions there that do not use lodash. And why use the spread operator when you don't need to, or if it doesn't work? – Heretic Monkey Feb 07 '19 at 22:21
  • 1
    Better focus on readability, rather than using spread in this case – baao Feb 07 '19 at 22:23
  • 2
    because spread is hipster. Being unreadable is the new cool – Kevin B Feb 07 '19 at 22:24

1 Answers1

2

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);
Kosh
  • 16,966
  • 2
  • 19
  • 34