I have an array of objects and I'd like to push an element at the beginning of the of the array.
I have this:
var TheArray = TheObjects.Array;
TheArray.push(TheNewObject);
It's adding TheNewObject at the end. Do I need to create a new array, add TheNewObject to it and then loop through TheArray and add each element the to the array?
Answer
Use unshift
, which modifies the existing array by adding the arguments to the beginning:
TheArray.unshift(TheNewObject);
No comments:
Post a Comment