0

I have few arrays with objects and I need to merge them in to one, but next code doesn't works, it return empty array:

let y = [{id:1, value: 'test1'}, {id:2, value: 'test2'}]; let z = [{id:3, value: 'test3'}, {id:4, value: 'test4'}]; let x = [{id:5, value: 'test5'}, {id:6, value: 'test6'}]; let arr = []; arr.concat(...x,...y,...z); console.log(arr);

3
  • let arr = [...y, ...x, ...z]; Commented Sep 14, 2020 at 12:55
  • 1
    arr.concat returns a new array…!? It does not modify arr in place. Commented Sep 14, 2020 at 12:55
  • 1
    concat creates a new array. Change it to arr.push(...x,...y,...z) Commented Sep 14, 2020 at 13:01

1 Answer 1

1

Array.prototype.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array

let y = [{id:1, value: 'test1'}, {id:2, value: 'test2'}]; let z = [{id:3, value: 'test3'}, {id:4, value: 'test4'}]; let x = [{id:5, value: 'test5'}, {id:6, value: 'test6'}]; let arr = [].concat(...x,...y,...z); console.log(arr);

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

1 Comment

Or just [].concat(x,y,z)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.