Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove an arbitrary element from a JavaScript array
    primarykey
    data
    text
    <p>In my Google Maps application I can place markers on the map, and I keep a reference to each of the markers placed, along with some extra information in an array called <code>markers</code>.</p> <p>Adding markers is easy, I just <code>push()</code> the newly created object onto the array (<code>markers.push(marker)</code>);</p> <p>However, when it comes to removing an arbitrary marker from the array, given an index of the slot, it doesn't behave as expected. My function is:</p> <pre><code>function deleteMarker(markerIndex) { if (markerIndex!='' &amp;&amp; markerIndex&gt;=0 &amp;&amp; markerIndex&lt;markers.length) { if (confirm('Do you really want to remove this marker from the map?')) { alert('deleting marker '+markerIndex); //debugging purposes markers.splice (markerIndex, 1); } } } </code></pre> <p>I have no previous experience with the splice() function, but looking at <a href="http://www.w3schools.com/jsref/jsref_splice.asp" rel="nofollow noreferrer">its description @ w3schools</a> it seems to be pretty straight-forward. However, I get the following behaviour:</p> <p><code>markers.splice()</code> does nothing. So what am I doing wrong?</p> <p>And also, when <code>markerIndex</code> is 0 no confirmation box is shown. At first I assumed the lengthy if-condition evaluated to false and so the whole code block was skipped, however, using Firebug to step through the calls I found out that the condition holds (of course) for index 0 when array is non-empty, next step reveals that the <code>if (confirm(...))</code> and <code>alert('deleting...)</code> are <em>skipped</em> and <code>markers.splice()</code> is called (but nothing happens). This behaviour is so strange I decided to open this question.</p> <p><em>Can anyone please clarify what's going on?</em></p> <p>I thought that deleting markers will be the easiest bit of functionality one could do. I can add them, edit their contents, even <em>clear all markers</em> (<code>pop()</code>-ing markers off the <code>markers</code> array until empty) and all works nicely.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload