Why is my JS/jQuery loop stopping after 2 loops?
<p>So I have an array with a lot of repeat values. <code>myArr = ['yeh','yeh','yeh','hey']</code>. I have an HTML form that allows the user to type in a word and if that word is in the array it'll delete all the repeat values and print out the new myArr without those values. However, my loop keeps stopping after a few loops without finishing deleting the rest of the duplicates. like if I had <code>['yeh','yeh','yeh']</code> and typed "yeh" in the form, it only deletes two of the 'yeh'.</p>

<p>HTML</p>

<pre><code>&lt;form id="remove_user" action="#" method="post"&gt;
        &lt;label for="user_num"&gt;Remove user:&lt;/label&gt;
        &lt;input type="text" id="user_num" name="user_num" placeholder="number"&gt;
        &lt;input type="submit" value="(-) remove"&gt;
    &lt;/form&gt;&lt;!-- #remove_user --&gt;

    &lt;ul id="user_list"&gt;&lt;/ul&gt;&lt;!-- #user_list --&gt;
</code></pre>

<p>JS:</p>

<pre><code>$('#remove_user').submit(function(){
    id = $('#user_num').val();
    $.each(myArr, function(i, value){
        if (value == id){
            myArr.splice(i, 1);
            console.log(myArr);
        }
    });
    console.log(myArr);
    $('#user_list').html('');
    for (var i=0; i &lt; myArr.length; i += 1) {
        console.log(myArr[i]);
        $('#user_list').append('&lt;li&gt;' +myArr[i]+ '&lt;/li&gt;');
    };
    return false;
});
</code></pre>

<p>UPDATED ANSWER (so as said below, I was modifying it while looping over it, so things were getting skipped. Instead of what I did before, I used grep to create a new array of all the values that didn't match the submitted value, then just went through diffValues elements one by one and printed each out):</p>

<pre><code>$('#remove_user').submit(function(){
    id = $('#user_num').val();
    var diffValues = $.grep(myArr, function(value, i){
        return value != id;
    });
    console.log(diffValues);
    $('#user_list').html('');
    for (var i=0; i &lt; diffValues.length; i += 1) {
        console.log(diffValues[i]);
        $('#user_list').append('&lt;li&gt;' +diffValues[i]+ '&lt;/li&gt;');
    };
    return false;
});
</code></pre>
