For my web-application I would like to add an object before another object inside a self-defined object….
I did find the insertBefore method, but it only applies to DOM objects.
The object looks like:
objTemplate[0].objEntry;
objTemplate[1].objEntry;
<= add objEntry here
objTemplate[2].objEntry;
objTemplate[3].objEntry;
Now I would like to add a new instance of objEntry before entry 2 in objTemplate.
How can I do that?
Assuming
objTemplateis an array you can useArray.prototype.splice:Or, if, for whatever reason, it’s not a real array (and you’re using numerical indexes) then you can still use splice the following way:
This will work for “array-like” objects – note that your object must have a
lengthproperty.