How to push an array into an array inside an object in Javascript?
Question
There is an object like below:1
2
3
4var myObj = {
arrayOne: [],
arrayTwo: []
};
And one array is var newArray = ['A', 'B'];
I want to add newArray
into arrayOne: []
, the result should be like below1
2
3
4{
arrayOne: [['A', 'B']],
arrayTwo: []
};
What should I do?
Answer
There are two ways to add newArray
inside arrayOne
of myObj
.
obj.arrayOne.push(newArray);
obj['arrayOne'].push(newArray);
Reference
This is the end of post