Back
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.
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());
var arr = ["Jani", "Hege","Stale","Kai”,Jim","Borge"];
arr.splice(2, 0, "Lene");
console.log(arr.join());
Check out Web Development Online Courses and enroll today!
31k questions
32.8k answers
501 comments
693 users