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);
Ted
  • 1,682
  • 3
  • 25
  • 52

1 Answers1

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);
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
Mamun
  • 66,969
  • 9
  • 47
  • 59