0

How to push array in array via using Javascript ?

I know only push normal array like...

var arr = [];
arr.push(['one','two','three']);

That is..

array(
 'one',
 'two',
 'three'
)

But what about ? How to push like this...

array(
 array(
  'one',
  'one_two'
 ),
 'two',
 'three'
)

3 Answers 3

2

That's what you are doing already.

This code makes a single array:

var arr = [];
arr.push('one','two','three'); // push three items

I.e. the same result as:

var arr = ['one','two','three'];

This code makes a jagged array (an array in an array):

var arr = [];
arr.push(['one','two','three']); // push one item that is an array

I.e. the same result as:

var arr = [
  ['one','two','three']
];
Sign up to request clarification or add additional context in comments.

1 Comment

@Guffa that was clear for me to know what I just did. As OP was interested in pushing the array in an array I did that. Thanks you. +1.
2

like this.

arr.push([['one','two','three']]);

1 Comment

That makes an array in an array in an array, i.e. the same as var arr = [ [ ['one','two','three'] ] ];
0

You can also use

var oldArray = new Array() // Put something inside

var newArray = {a:valueA, b:valueB, c:valueC}

oldArray.push(newArray)

Best

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.