/ JAVASCRIPT, FRONT-END

How to push an array into an array inside an object in Javascript?

Question

There is an object like below:

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

{ 
    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