简体   繁体   English

在jQuery中,如何在每个循环中动态删除数组元素?

[英]In jQuery, how can I in an each loop remove array elements dynamically?

Let's say I have an array : 假设我有一个数组:

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

and I'm looping them with jQuery.each: 我用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? 我该如何实现?

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. 'filteredarr'将是另一个具有满足条件的数字的数组。

Working Demo 工作演示

OR 要么

Try .splice() as shown : 尝试.splice() ,如下所示:

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. 注意:-如果数组不包含重复的数字(如在问题中指定的发问者),则后两个答案绝对可以正常工作,并且前两个答案在任何情况下均适用。

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. 您不应在数字数组上使用$(arr) ,并且在遍历数组时也不应从数组中删除元素。 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): 而不是jQuery和each() ,应该使用Array.prototype.filter (如果需要支持较旧的浏览器或绝对希望使用jQuery,也可以使用$.grep() ):

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). 调用此方法后, filtered将是一个数组,其中包含somethingIsTrue不为true(这是我认为您要的内容)的所有项目。

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. 如上所述,jQuery的each不是在这里使用的工具,但一般而言,您应该只使用$(arr)arr只包含DOM元素。 To iterate over an ordinary array in jQuery, use $.each(arr, callback) . 要遍历jQuery中的普通数组,请使用$.each(arr, callback)

working example 工作实例

try this fiddle, http://jsfiddle.net/devools/35aysq6t/ 试试这个小提琴, 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 + ',');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM