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
  • 151,816
  • 78
  • 307
  • 352
SmRndGuy
  • 1,719
  • 5
  • 30
  • 49
  • 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
  • 320,036
  • 81
  • 464
  • 592
2

Use push:

arr.push([7,8,9]);
Digital Plane
  • 37,354
  • 7
  • 57
  • 59
1

Try this:

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

push() is a standard array method

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

Try this:

var arr=[  
[1,2,3]  
[4,5,6]  
] ;
arr.push([7,8,9]);
Chandu
  • 81,493
  • 19
  • 133
  • 134
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
  • 155,703
  • 32
  • 311
  • 293
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,182
  • 12
  • 47
  • 90