Javascript Function to Insert Value in Array at Specific Position

by Sasikumar 2014-09-03 18:00:24

We can use the following function to insert array item at specific position in array :
Javascript Function :
Array.prototype.insert = function (index, item) {
    this.splice(index, 0, item);
};
Usage Example :
var myArray = [ 'hiox', 'hibihi', 'eluthu', 'easycalculation' ];
myArray.insert(3, 'hioxindia');

// output array
// [ 'hiox', 'hibihi', 'eluthu', 'hioxindia', 'easycalculation' ]

This is written as prototype function and hence can be used directly as shown in example.
1710
like
0
dislike
0
mail
flag

You must LOGIN to add comments