Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:50:30+00:00 2026-06-09T22:50:30+00:00

So I have an array with a lot of repeat values. myArr = [‘yeh’,’yeh’,’yeh’,’hey’]

  • 0

So I have an array with a lot of repeat values. myArr = ['yeh','yeh','yeh','hey']. 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 ['yeh','yeh','yeh'] and typed “yeh” in the form, it only deletes two of the ‘yeh’.

HTML

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

    <ul id="user_list"></ul><!-- #user_list -->

JS:

$('#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 < myArr.length; i += 1) {
        console.log(myArr[i]);
        $('#user_list').append('<li>' +myArr[i]+ '</li>');
    };
    return false;
});

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

$('#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 < diffValues.length; i += 1) {
        console.log(diffValues[i]);
        $('#user_list').append('<li>' +diffValues[i]+ '</li>');
    };
    return false;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:50:30+00:00Added an answer on June 9, 2026 at 10:50 pm

    When you splice the array, you are changing that array. So basically your array is going through these steps:

    ['yeh','yeh','yeh','hey']
      ^^^
    splice, move iterator forward
    ['yeh','yeh','hey']
            ^^^
    splice, move iterator forward
    ['yeh','hey']
                  ^^^
    end loop - one of the 'yeh' is still there
    

    You might be interested in .grep().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that has a lot of values including a lot of
i have an array that looks like that : Array ( [0] => Array
I have a array which contains values like this See below for script sample
I have an array of data that is coming in to the VBA code
i have been trying to write a program to insert the array values in
I have this array at the moment which contains a lot of data URIs.
Hey all I have a large html string like <a style=background: rgb(100, 101, 43)
i have an array that looks like this: foreach($obj as $key => $value) {
I have an array which looks like $cclArray[0]['url']='test.html'; $cclArray[0]['entity_id']=9; $cclArray[1]['url']='test1.html'; $cclArray[1]['entity_id']=10; $cclArray[2]['url']='test2.html'; $cclArray[2]['entity_id']=11; if
Often, I will have an array holding a set of values each of which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.