Question

There is an object like below:

1
2
3
4
var myObj = {
arrayOne: [],
arrayTwo: []
};

And one array is var newArray = ['A', 'B'];

I want to add newArray into arrayOne: [], the result should be like below

1
2
3
4
{ 
arrayOne: [['A', 'B']],
arrayTwo: []
};

What should I do?

Answer

There are two ways to add newArray inside arrayOne of myObj.

  1. obj.arrayOne.push(newArray);
  2. obj['arrayOne'].push(newArray);

Reference


This is the end of post