Topic: inserting into javascript/prototype arrays

Hello,

What is the best way to insert a value into the middle of an array?
Push puts values at the end.
Unshift inserts to the beginning.
[3] overwrites an item.
So how to _insert_ to a certain position?

Thanks,
Mathijs

Re: inserting into javascript/prototype arrays

You can do this:

arr.insert(2, 'foo') # inserts it before the element with index 2

Railscasts - Free Ruby on Rails Screencasts

Re: inserting into javascript/prototype arrays

I feel ashamed... sooo easy smile
I'm currently working through the excellent "Prototype & Scriptaculous in action" book, but it only basically covers javascript(without prototype) so it was not in there...

thanks anyway!

Re: inserting into javascript/prototype arrays

Oops, I just now looked at the title. I thought you were referring to ruby arrays. Does it work though?

Railscasts - Free Ruby on Rails Screencasts

Re: inserting into javascript/prototype arrays

uhm... nope smile
but for the moment I just create a new array using:
arr.slice(0,1).concat(4).concat(arr.slice(1))

not very pretty, but it works