JS: quickly removing an arbitrary element from an Array

Unless I am mistaken (and I could be), Arrays in JavaScript have no direct way to delete an arbitrary element. I had a little problem where I knew the index of the item I want to remove, there was no easy way to do this. I can’t just say ‘array.remove(5)’.
A way that I have seen some people use get around this problem is to use Array.prototype.join to push the contents into a string, then do a string replace on an element value and then String.


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan

<p>Unless I am mistaken (and I could be), Arrays in JavaScript have no direct way to delete an arbitrary element. I had a little problem where I knew the index of the item I want to remove, there was no easy way to do this. I can't just say 'array.remove(5)'.</p> <p>A way that I have seen some people use get around this problem is to use Array.prototype.join to push the contents into a string, then do a string replace on an element value and then String.split to produce the array with the element removed. It is a bit of a hack. Heck, it is a lot of a hack!</p> <p>If your browser supports parts of ECMA Script 5, then Array object now has some extra methods for you to use, including filter(). Filter allows you to specify a function that is called for every item in the list, and for that value you can say if you want it in the result set. So it is ideal to solve the problem of removing an element from the list if you know it's value.</p> <div class="CodeRay"> <div class="code"><pre><span class="keyword">var</span> valToRemove = <span class="string"><span class="delimiter">"</span><span class="content">hello</span><span class="delimiter">"</span></span>; <span class="keyword">var</span> values = [<span class="string"><span class="delimiter">"</span><span class="content">Ah</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">hello</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">world</span><span class="delimiter">"</span></span>]; values = values.filter(<span class="keyword">function</span>(i) { <span class="keyword">if</span>(i != valToRemove) <span class="keyword">return</span> <span class="predefined-constant">true</span>; <span class="keyword">else</span> <span class="keyword">return</span> <span class="predefined-constant">false</span>; });</pre></div> </div> <p>Pretty easy, and quick given a small array. Obviously, this little piece of code will remove every element from the list with the value I am looking for – but this is ok for my code.</p> <p>Still, there isn't an easy way to remove the nth element from a list unless you shift or pop your way through the array. But that is another story.</p>


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan


Print Share Comment Cite Upload Translate Updates
APA

Paul Kinlan | Sciencx (2010-11-28T00:00:00+00:00) JS: quickly removing an arbitrary element from an Array. Retrieved from https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/

MLA
" » JS: quickly removing an arbitrary element from an Array." Paul Kinlan | Sciencx - Sunday November 28, 2010, https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/
HARVARD
Paul Kinlan | Sciencx Sunday November 28, 2010 » JS: quickly removing an arbitrary element from an Array., viewed ,<https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/>
VANCOUVER
Paul Kinlan | Sciencx - » JS: quickly removing an arbitrary element from an Array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/
CHICAGO
" » JS: quickly removing an arbitrary element from an Array." Paul Kinlan | Sciencx - Accessed . https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/
IEEE
" » JS: quickly removing an arbitrary element from an Array." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/. [Accessed: ]
rf:citation
» JS: quickly removing an arbitrary element from an Array | Paul Kinlan | Sciencx | https://www.scien.cx/2010/11/28/js-quickly-removing-an-arbitrary-element-from-an-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.