0 votes
1 view
in Web Technology by (31.4k 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 (73.5k 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"); 

console.log(arr.join());

...