-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.

7
  • Possible duplicate of Group array of object nesting some of the keys with specific names Commented Feb 7, 2019 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. Commented Feb 7, 2019 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. Commented Feb 7, 2019 at 22:19
  • 1
    Better focus on readability, rather than using spread in this case Commented Feb 7, 2019 at 22:23
  • 2
    because spread is hipster. Being unreadable is the new cool Commented Feb 7, 2019 at 22:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

It's a shame that js allows syntax like that
@bambam, "it's both a gift and a curse".
:-) True. Here it's a curse
thx, upvote for answering the actual question. Next time I'll ask if spread is useful, beautiful or hipster, but for now just - thanks
@silverfighter, happy to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.