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 because I can't believe the trouble I'm having finding such a basic function! Thanks for your help.
From stackoverflow
-
What you want is the splice function on the native array object. Quoted from the referenced link:
In this example we will create an array and add an element to it:
<script type="text/javascript"> var arr = new Array(5); arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; document.write(arr + "<br />"); arr.splice(2,0,"Lene"); document.write(arr + "<br />"); </script>The output of the code above will be:
Jani,Hege,Stale,Kai Jim,Borge Jani,Hege,Lene,Stale,Kai Jim,Borgetags2k : Thanks, I thought I would feel stupid for asking but now that I know the answer I don't! Why on earth did they decide to call it splice when a more searchable term was in common use for the same function?!Christoph : @tags2k: because the function does more than inserting items and it's name was already established in perl?tags2k : Ahh, Perl. That explains the obfuscation then ;-)
0 comments:
Post a Comment