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
  • 313,535
  • 40
  • 245
  • 254

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,066
  • 2
  • 17
  • 29
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,526
  • 16
  • 61
  • 110