[UPDATE] Thanks to Joe and others hinting me to the danger of purely using indexOf() without a check for -1. Added it in the last example.
I mean it’s not the first time I am doing it, but it happens always again that I have to look up how exactly it works. And every time I know it is very easy.
So here is how to remove an element from an array, mainly for my poor brain, so it doesn’t have to remember anymore. Though I know, now that I wrote it down I will never forget it again.
Unfortunately the most obvioous solution leaves a “hole” in the array :-(.
>>> var list = [4,5,6]; >>> delete list; true >>> list [4, undefined, 6]
Sad, but understandable.
So do it right and use splice().
>>> var list = [4,5,6]; >>> list.splice(1, 1); // Remove one element, returns the removed ones.  >>> list [4, 6]
Actually, what I mostly need is: remove a certain value from an array.
I.e. I have some list of visible IDs and I want to remove one because it’s not visible anymore. So just extend the above example a bit.
>>> var visibleIds = [4,5,6]; >>> var idx = visibleIds.indexOf(5); // Find the index >>> if(idx!=-1) visibleIds.splice(idx, 1); // Remove it if really found!  >>> visibleIds [4, 6]
I hope I didn’t bore anyone with that