0

Im trying to combine two sets of data.

var a = [{ Id: 1, Name: 'foo' },
  { Id: 2, Name: 'boo' }];

var b = [{ Id: 3, Name: 'doo' },
        { Id: 4, Name: 'coo' }];

Most of question here i found is only a normal array.

I've tried Object.assign(a, b); but it only returns the b value.

The a and b data is from the server side.

Thanks for the help.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320

2 Answers2

1

Try array concat

var a = [{ Id: 1, Name: 'foo' },
  { Id: 2, Name: 'boo' }];

var b = [{ Id: 3, Name: 'doo' },
        { Id: 4, Name: 'coo' }];
let c = a.concat(b);

console.log(c);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33
1

Using spread syntax

var a = [{ Id: 1, Name: 'foo' },
  { Id: 2, Name: 'boo' }];

var b = [{ Id: 3, Name: 'doo' },
        { Id: 4, Name: 'coo' }];

c = [...a, ...b];

Note: Spread syntax is not supported by all browsers , its ok if you use es6/5 compiler though like babel. See Spread

Another option is

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116