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
l2aelba
22.3k23 gold badges108 silver badges145 bronze badges
3 Answers 3
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
Guffa
703k112 gold badges760 silver badges1k bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Vinay
@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.
like this.
arr.push([['one','two','three']]);
answered Apr 25, 2013 at 7:55
Vinay
6,8774 gold badges34 silver badges51 bronze badges
1 Comment
Guffa
That makes an array in an array in an array, i.e. the same as
var arr = [ [ ['one','two','three'] ] ];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
xhallix
3,0115 gold badges41 silver badges57 bronze badges
Comments
lang-js