Array.prototype.splice and a schoolboy error.

The other day I posted about there being no ‘delete an arbitrary element’ method on Array in Javascrip. The problem being that I tried the solution I am about to present, but made the biggest School Boy Error possible – I didn’t read the documentation correctly!
Whilst I maintain that my solution removes the need to first find the elements, and then delete them (which is better ;). It must be noted that Array.


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

<p>The other day I <a href="https://paul.kinlan.me/js-quickly-removing-an-arbitrary-element-from">posted</a> about there being no 'delete an arbitrary element' method on Array in Javascrip. The problem being that I tried the solution I am about to present, but made the biggest School Boy Error possible – I didn't read the documentation correctly!</p> <p>Whilst I maintain that my solution removes the need to first find the elements, and then delete them (which is better ;). It must be noted that Array.prototype.splice allows you to remove arbitrary elements if you know the index and the number of elements you want to remove.</p> <p>Anyway, here goes, to remove 1 element from an Array from an arbitrary position:</p> <div class="CodeRay"> <div class="code"><pre><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>]; <span class="keyword">var</span> result = values.splice(<span class="integer">1</span>,<span class="integer">1</span>); console.log(values); console.log(result);</pre></div> </div> <p>This removes the 'hello' from the values array in place and returns the elements removed. The result is values = ['Ah', 'world'] and result = ['hello']</p> <p>Thanks to @<a href="http://www.twitter.com/dezfowler">dezfowler</a></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-30T00:00:00+00:00) Array.prototype.splice and a schoolboy error.. Retrieved from https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/

MLA
" » Array.prototype.splice and a schoolboy error.." Paul Kinlan | Sciencx - Tuesday November 30, 2010, https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/
HARVARD
Paul Kinlan | Sciencx Tuesday November 30, 2010 » Array.prototype.splice and a schoolboy error.., viewed ,<https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/>
VANCOUVER
Paul Kinlan | Sciencx - » Array.prototype.splice and a schoolboy error.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/
CHICAGO
" » Array.prototype.splice and a schoolboy error.." Paul Kinlan | Sciencx - Accessed . https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/
IEEE
" » Array.prototype.splice and a schoolboy error.." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/. [Accessed: ]
rf:citation
» Array.prototype.splice and a schoolboy error. | Paul Kinlan | Sciencx | https://www.scien.cx/2010/11/30/array-prototype-splice-and-a-schoolboy-error/ |

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.