Array push element

hi,
i need to add ‘GGG’ to an existing array, but at a certain position without overwriting other elements. is there a way to do this without looping over the array and reinsert the elements?

thanks

pete

existing array: ‘XXX’,‘YYY’,‘TTT’);
final array should be: ‘XXX’,‘GGG’,‘YYY’,‘TTT’);



var myArray = new Array('XXX','YYY','TTT');

//get position of YYY
var pos = myArray.indexOf('YYY');

//add GGG at the postion of YYY
myArray[pos] = 'GGG';

// how can i readd YYY,TTT


Use splice.

var myArray = ['XXX','YYY','TTT'];
var pos = myArray.indexOf('YYY');

myArray.splice(pos,0, 'GGG');

// the second argument is the number of items to remove- 0 in this case

alert(myArray)

/* returned value: (Array)
XXX,GGG,YYY,TTT
*/