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'
)
asked Apr 25, 2013 at 7:53

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']
];
answered Apr 25, 2013 at 7:56
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']]);
answered Apr 25, 2013 at 7:55

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

answered Apr 25, 2013 at 8:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.