0 votes
1 view
in Web Technology by (47.8k points)

I am looking for a JavaScript array insert method, in the style of:

arr.insert(index, item)

Preferably in jQuery, but any JavaScript implementation will do at this point.

1 Answer

0 votes
by (107k points)

To insert an item into an array at a specific index in JavaScript you can use the splice function.

arr.splice(index, 0, item); 

This will insert item into array at the specified index by deleting 0 items first, that is, it's just an insert.

See the example below:-

var arr = ["Jani", "Hege","Stale","Kai”,Jim","Borge"]; 

arr.splice(2, 0, "Lene"); 


Related questions

0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !