简体   繁体   English

jQuery遍历循环删除元素

[英]jQuery iterate through loop delete elements

I have a javascript array of objects: each object contains key/value pairs. 我有一个对象的javascript数组:每个对​​象都包含键/值对。 I'm trying to iterate through this array, and delete any object whose value for a particular key (say "Industry") fails to match a given value. 我试图遍历此数组,并删除其特定键的值(例如“ Industry”)的值与给定值不匹配的任何对象。 Here is my code, for some reason it's not looping through the whole array, and I think it has something to do with the fact that when I delete an item the loop counter is botched somehow: 这是我的代码,由于某种原因,它没有遍历整个数组,并且我认为这与以下事实有关:删除项目时,循环计数器会以某种方式被破坏:

var industry = 'testing';
var i = 0;
for (i = 0; i < assets_results.length; i++) {

    var asset = assets_results[i];
    var asset_industry = asset['industry'];
    if (industry != asset_industry) { assets_results.splice(i,1); }

}   

Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

This is because when you splice one element, the size of array decreases by one. 这是因为当您拼接一个元素时,数组的大小将减少一。 All elements after the splice shift one position to the beginning of the array and fills the space of spliced one. 拼接后的所有元素将一个位置移至数组的开头,并填充拼接后的空间。 So the code misses one element.Try this code. 因此代码缺少一个元素。请尝试此代码。

var industry = 'testing';
var i = 0;
for (i = 0; i < assets_results.length; i++) {

    var asset = assets_results[i];
    var asset_industry = asset['industry'];
    if (industry != asset_industry) { 
              assets_results.splice(i,1); 
              i--;
    }

} 

splice removes an element from an array and resizes it : splice从数组中删除一个元素并调整其大小:

var arra = ['A', 'B', 'C', 'D'];
arr.splice(1,2); // -> ['A', 'D'];

Which means that you should not increment i when you splice, because you skip the next element. 这意味着您在拼接时不应该增加i,因为您跳过了下一个元素。 splicing will make of the i + 2 element the i + 1 element. 拼接将使i + 2元素成为i +1元素。

var industry = 'testing';

for (var i = 0, max = assets_results.length; i < max; ) { // Accessing a property is expensive.
    if (industry != assets_results[i]['industry']) {
        assets_results.splice(i,1);
    } else {
        ++i;
    }
}   

This is a common problem when modifying an object while iterating through it. 在迭代对象时修改对象时,这是一个常见问题。 The best way to avoid this problem is, rather than deleting pairs from the existing array if they fail the test, to create a new array and only add pairs if they pass the test. 避免此问题的最佳方法是创建一个新数组,并仅在通过测试的情况下才添加对,而不是从现有阵列中删除对。

var industry = 'testing';
var i = 0;
var asset_results_filtered = [];

for (i = 0; i < assets_results.length; i++) {
    if (industry == assets_results[i]) {
        asset_results_filtered.push(assets_results[i]);
    }
}

EDIT: Your code looked a bit illogical — I modified the example to use the variables given. 编辑:您的代码看起来有点不合逻辑-我修改了示例以使用给定的变量。

Try this instead: 尝试以下方法:

var industry = 'testing';
var i = assets_results.length - 1;
for (; i > 0; i--) {

    var asset = assets_results[i],
        asset_industry = asset['industry'];
    if (industry != asset_industry) { assets_results.splice(i,1); }

}

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

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