-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
  • 14,775
  • 3
  • 30
  • 41
Pablo
  • 8,001
  • 13
  • 50
  • 74

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,858
  • 11
  • 16
1

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

ps-aux
  • 10,627
  • 21
  • 73
  • 123