1

Let's say I have an array :

var arr = [1,2,3,4,5];

and I'm looping them with jQuery.each:

$(arr).each(function(index, value){
    if (somethingIsTrue){
        // Based on if "somethingIsTrue" I want to remove this particular item from the array at this point
    }
});

How can I achieve this?

12
  • 2
    how about creating a new array with the elements that satisfies somethingIsTrue? Commented Sep 16, 2014 at 10:24
  • pop only removes the last element in the array. Commented Sep 16, 2014 at 10:26
  • Sachin, I know you didn't answer the question but I ended up going with your solution instead. Much easier to just append new values to the new array. Commented Sep 16, 2014 at 11:45
  • @Weblurk It should be noted that the accepted answer will not work correctly if the array has more than one item that matches the specified condition. See here: jsfiddle.net/zdjqh8om/2 Commented Sep 16, 2014 at 12:38
  • plz see updated answer..as per your question first answer will work absolutely fine but for case specified in above fiddle link try my second answer. Commented Sep 16, 2014 at 13:19

3 Answers 3

1
var arr = [1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12];
var arr1 = []   //make a separate array to store numbers fulfilling specified condition

$.each(arr,function(i,v){
    if(somethingIsTrue){
    arr1.push(v)  //fill second array with required numbers fulfilling specified condition
    }
});

Working Demo

OR

var arr = [1,2,2,2,3,4,5,5,5,6,7,8];
var filteredarr = $.map(arr,function (value,index) {
  return (somethingIsTrue ? value : null)
});

'filteredarr' will be a another array having numbers satisfying the condition.

Working Demo

OR

Try .splice() as shown :

var arr = [1,2,3,4,5];

$(arr).each(function(index, value){
  if(somethingIsTrue){
    arr.splice(index,1)
  }
});

Working Demo

OR

$.each(arr,function(i,v){
  if(somethingIsTrue){
  var i1 = arr.indexOf(v);
    arr.splice(i1,1);
  }
});

Working Demo

NOTE :- Last two answers will work absolutely fine if array do not contains repeating numbers(as questioner specified in question) and first two answers will work in any scenario.

Sign up to request clarification or add additional context in comments.

9 Comments

You shouldn't use $(arr) with an array of numbers. Also, even if it works, I think it's not a good idea to remove elements from an array while you are iterating through it.
the above shown answer is absolutely correct...its arr only not $(arr) .. i m removing specified element from arr only..and please downvote if answer is not correct and not on your personnal opinion that 'its not a good idea'..@JLRishe
I'm not sure how you are trying to claim that you're not using $(arr), when there's an $(arr) in your code, plain as day. On that point, your post is incontrovertibly incorrect. Aside from that, anyone has the prerogative to downvote answers that are using bad practices, and removing elements from an array while iterating through it is a bad practice.
@JLRishe..i just remove the specified element from array when it just iterated already...so its not a problem..and moreover i have specified the fiddle link also just test it as many times as you want.
Actually, it very much is a problem. Please see here to observe your code producing a completely wrong result: jsfiddle.net/zdjqh8om/1
|
1

There are a number of problems with the way you're trying to go about this. You shouldn't use $(arr) on an array of numbers, and you shouldn't remove elements from an array while you are iterating through it. That's a surefire recipe for buggy code.

Instead of jQuery and each(), you should use Array.prototype.filter (you can also use $.grep() if you need to support older browsers or absolutely want to use jQuery):

var filtered = arr.filter(function (value) {
    return !somethingIsTrue;
});

After this is called, filtered will be an array containing all the items where somethingIsTrue is not true (which is what I think you are asking for).

As stated above, jQuery each is not the tool to use here, but in general you should only use $(arr) when arr contains only DOM elements. To iterate over an ordinary array in jQuery, use $.each(arr, callback).

working example

Comments

0

try this fiddle, http://jsfiddle.net/devools/35aysq6t/

var arr = [1, 2, 3];


$(arr).each(function (index, value) {
    if (value = 2) {
        arr = jQuery.grep(arr, function(value) {
            return value != 2;
        });
    }
});

$(arr).each(function (index, value) {
    $('.array').append(value + ',');
});

1 Comment

if (value = 2) will always execute its body. Also, this seems like a rather convoluted (and incorrect) way to accomplish this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.