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 7191427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:43:36+00:00 2026-05-28T19:43:36+00:00

var results = [‘one’, ‘two’, ‘one hundred’, ‘three’]; var removal = []; $.each(results, function(i)

  • 0
var results = ['one', 'two', 'one hundred', 'three'];
var removal = [];
$.each(results, function(i) {
    removal.push(i);
    if (results[i].indexOf('one') == -1){
        console.log('Removing:' + results[i] + '(' + removal[i] + ')');
        results = results.splice(removal[i], 1);
    }
});

I have the following code, but it is breaking after it removes the first result.

I want it to remove anything that does not contain the word ‘one’.

I am guessing it is breaking because the removal order changes once one has been removed.

What am I doing wrong?

  • 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-05-28T19:43:37+00:00Added an answer on May 28, 2026 at 7:43 pm

    You shouldn’t splice the Array while you’re iterating it with $.each().

    Since you’re changing the length of the Array, you’re going beyond the final index since.

    Just use a for loop and adjust i when you remove an item…

    var results = ['one', 'two', 'one hundred', 'three'];
    var removal = [];
    for(var i = 0; i < results.length; i++) {
        removal.push(i);
        if (results[i].indexOf('one') == -1){
            console.log('Removing:' + results[i] + '(' + removal[i] + ')');
            results.splice(i, 1);
            i--;
        }
    };
    

    Note that I changed this…

    results = results.splice(removal[i], 1);
    

    to this…

    results.splice(removal[i], 1);
    

    You don’t want that since splice() modifies the original, and returns the item(s) removed.

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

Sidebar

Related Questions

I have a Linq to Entities query like this one: var results = from
How do you combine two jQuery search results? eg: var $allFoos = $('.foo'), $allBars
I'd like to write this function: function getResults(nums){ var results = []; for(var i
I have the following function. function checkWinner (results) { for (var i = 0;
I have the following code - <script type=text/javascript> function test() { var results =
var results = from formNumber in context.DetailTM join c in context.ClaimPeriodTM on formNumber.ClaimPeriod equals
Take the following linq query: var summary = results.Select(r => new { TotalPopulation =
The following code results in undefined for lastIndex: var a = /cat/g; var l
When running on an xhtml page, xpath doesn't seem to return any results. var
i have this in my query: var results = (from urls in _context.Urls join

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.