-1

I know this has been asked a lot of times, but I cannot get it to work.

I have an empty array a

var a = [];

and an array with an object b

var b = [{
   title: 'test'
}]

I want to join them so a will look exactly like b. The idea is to do this inside a for loop so a would be added a new item each time.

By using a.concat(b), a results in an empty array.

Not sure what I am missing.

SLePort
  • 15,211
  • 3
  • 34
  • 44
Pablo
  • 9,424
  • 17
  • 55
  • 78

2 Answers2

4

Per Array.prototype.concat()

This method does not change the existing arrays, but instead returns a new array.

You need to assign this operation back to a

a = a.concat(b)

Nick G
  • 1,953
  • 12
  • 16
1

You need to assign result of that call to a. a = a.concat(b)

ps-aux
  • 11,627
  • 25
  • 81
  • 128