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?

2

6 Answers 6

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

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

arr.splice(offset,0,thing_to_insert);
Sign up to request clarification or add additional context in comments.

Comments

2

Use push:

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

Comments

1

Try this:

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

push() is a standard array method

Comments

1

Try this:

var arr=[  
[1,2,3]  
[4,5,6]  
] ;
arr.push([7,8,9]);

Comments

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]);

Comments

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);

1 Comment

arr after your last code would look like this: [[1,2,3],[4,5,6],7,8,9] ... which is not correct.

Your Answer

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.