简体   繁体   English

从jQuery每个循环中删除item [i]

[英]Remove item[i] from jQuery each loop

How do I remove an item[i] from items once it reaches in: 如何在项目到达时从项目中删除项目[i]:

$.each(items, function(i) {
    // how to remove this from items
});

It would be better not to use $.each in this case. 在这种情况下,最好不要使用$.each Use $.grep instead. 请改用$.grep This loops through an array in pretty much the same way as $.each with one exception. 这循环遍历一个数组,与$.each ,只有一个例外。 If you return true from the callback, the element is retained. 如果从回调中返回true ,则保留该元素。 Otherwise, it is removed from the array. 否则,它将从阵列中删除。

Your code should look something like this: 您的代码应如下所示:

items = $.grep(items, function (el, i) {
    if (i === 5) { // or whatever
        return false;
    }

    // do your normal code on el

    return true; // keep the element in the array
});

One more note: this in the context of a $.grep callback is set to window , not to the array element. 还有一点需要注意: this$.grep回调的上下文中设置为window ,而不是数组元素。

I'm guessing you want $.map . 我猜你想要$.map You can return null to remove an item, and not worry about how indices might shift: 您可以return null以删除项目,而不必担心索引可能会如何移动:

items = $.map(items, function (item, index) {
    if (index < 10) return null; // Removes the first 10 elements;
    return item;
});

If you want to remove an element from array, use splice() 如果要从数组中删除元素,请使用splice()

var myArray =['a','b','c','d'];
var indexToRemove = 1;
// first argument below is the index to remove at, 
//second argument is num of elements to remove
myArray.splice(indexToRemove , 1);

myArray will now contain ['a','c','d'] myArray现在包含['a','c','d']

the solution is below: 解决方案如下:

_.each(data, function (item, queue) {
    if (somecondition) {
        delete data[queue]
    }
});

Something like 就像是

var indexToBeRemoved = 3; // just an illustration
$.each(items, function(i) {
    if(i==indexToBeRemoved){
        $(this).remove();
    }
});

As mentioned by @lonesomday above (I simply couldn't add this in a comment) grep is for Arrays, but you could insert your selector inside grep : 正如上面的@lonesomday所提到的(我根本无法在注释中添加它) grep用于数组,但你可以在grep插入你的选择器:

var items = $.grep($(".myselector", function (el, i) {
  return (i===5) ? false : true;
};

This would store all elements found using $(".myselector") in ìtems` leaving out the item at the 6th position (the list is 0 indexed, which makes "5" the 6th element) 这将存储使用$(".myselector")在“茎$(".myselector")找到的所有元素,在第6个位置省略该项(列表为0索引,这使得“5”成为第6个元素)

Although I would typically prefer using $.grep() to filter the array, I have an instance where I'm already using $.each() on the array to process a dataset. 虽然我通常更喜欢使用$.grep()来过滤数组,但我有一个实例,我已经在数组上使用$.each()来处理数据集。 After doing some processing, I can determine whether or not the item needs to be removed from the array: 在做了一些处理之后,我可以确定是否需要从数组中删除该项:

// WARNING - DON'T DO THIS:
$.each(someArray, function(index, item) {
    // Some logic here, using 'item'

    if (removeItem) {
        // Spice this item from the array
        someArray.splice(index, 1)
    }

    // More logic here
});

WARNING: This presents a new problem! 警告:这是一个新问题! Once the item has been spliced from the array, jQuery will still loop for the length of the original array. 一旦项目从数组中拼接,jQuery仍将循环显示原始数组的长度。 Eg: 例如:

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

$.each(foo, function(i, item) {
    console.log(i + ' -- ' + item);
    if (i == 3){ 
        foo.splice(i, 1); 
    }
});

Will output: 将输出:

0 -- 1
1 -- 2
2 -- 3
3 -- 4
4 -- undefined

And foo is now [1, 2, 3, 5] . foo现在是[1, 2, 3, 5] 1,2,3,5 [1, 2, 3, 5] Every item in the array is "shifted" relative to the jQuery loop, and we missed the element "5" altogether, and the last item in the loop is undefined . 数组中的每个项都相对于jQuery循环“移位”,我们完全错过了元素“5”,并且循环中的最后一项是undefined The best way to solve this is to use a reverse for loop (going from arr.length - 1 to 0 ). 解决这个最好的办法是使用反向for循环(从去arr.length - 10 )。 This will ensure that removing an element won't affect the next item in the loop. 这将确保删除元素不会影响循环中的下一个项目。 However since the question here is with respect to $.each, there are a few alternative ways of solving this: 但是,由于这里的问题是关于$ .each,因此有一些替代方法可以解决这个问题:

1) $.grep() the array before looping 1) $.grep()循环前的数组

var someArray = $.grep(someArray, function(item) {
    // Some logic here, using 'item'

    return removeItem == false;
});

$.each(someArray, function(index, item) {
    // More logic here
});

2) Push items into another array 2)将项目推送到另一个阵列

var choiceArray = [ ];

$.each(someArray, function(index, item) {
    // Some logic here, using 'item'

    if (removeItem) {
        // break out of this iteration and continue
        return true; 
    }

    // More logic here

    // Push good items into the new array
    choiceArray.push(item);
});

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

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