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.

<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>


Print Share Comment Cite Upload Translate
APA
Paul Kinlan | Sciencx (2024-03-29T09:22:17+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 2024-03-29T09:22:17+00:00,<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 2024-03-29T09:22:17+00:00]. 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 2024-03-29T09:22:17+00:00. 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: 2024-03-29T09:22:17+00:00]
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/ | 2024-03-29T09:22:17+00:00
https://github.com/addpipe/simple-recorderjs-demo