3

If I have for example this array-variable:

var arr=[  
[1,2,3]  
[4,5,6]  
]  

How can I insert something in that array, for example like this:

arr=[  
[1,2,3]  
[4,5,6]   
[7,8,9]  
] 

I tried arr=[arr,[7,8,9]] but then the [] are building up.

How to do this?

pimvdb
  • 146,912
  • 75
  • 297
  • 349
SmRndGuy
  • 1,609
  • 5
  • 29
  • 48
  • 1
    How about reading [documentation](https://developer.mozilla.org/en/JavaScript/Guide/Predefined_Core_Objects#Array_Object)? – Felix Kling Sep 20 '11 at 22:30
  • Duplicate. [How to add a new value to the end of an numerical array?](http://stackoverflow.com/questions/1961488/how-to-add-a-new-value-to-the-end-of-an-numerical-array) – user113716 Sep 21 '11 at 00:03

6 Answers6

4
arr.push([7,8,9]);

That should do the trick. If you want to insert:

arr.splice(offset,0,thing_to_insert);
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
2

Use push:

arr.push([7,8,9]);
Digital Plane
  • 35,904
  • 7
  • 55
  • 59
1

Try this:

arr.push([7,8,9]);

push() is a standard array method

Tomasz Nurkiewicz
  • 324,247
  • 67
  • 682
  • 662
1

Try this:

var arr=[  
[1,2,3]  
[4,5,6]  
] ;
arr.push([7,8,9]);
Chandu
  • 78,650
  • 19
  • 129
  • 131
0

Array.prototype.push adds elements to the end of an array.

var arr=[  
    [1,2,3]  
    [4,5,6]  
    ];

arr.push([7,8,9]);

Array.prototype.splice lets you add elements to the array at any index you desire:

var arr=[  
    [1,2,3]  
    [4,5,6]  
    ];

arr.splice(arr.length, 0, [7,8,9]);
Sean Vieira
  • 148,604
  • 32
  • 306
  • 290
0

You could use push?

I'm not sure if this would work:

arr.push([7,8,9]);

but I'm sure this would work:

arr.push(7);
arr.push(8);
arr.push(9);
IronicMuffin
  • 4,172
  • 11
  • 46
  • 87