2016-04-02 60 views
3

我有以下方面:接头上forEach循环不正常

https://jsfiddle.net/eqntaqbt/2/

obj.forEach(function(user, index){ 
    var userName = user.name; 
    console.log(index, userName); 

    if(index === 5 || index === 2){ 
    obj.splice(index, 1); 
    } 
}); 

我使用的是forEach环和splice以去除obj阵列上5位置和2的项目。但由于某种原因,它不能正常工作。

我在做什么错?

+0

'user.index' ..? – Rayon

+0

它工作正常..没有看到记录的用户名,请检查你的filddle中最后记录的数组。 –

+0

刚刚更新了问题 – gespinha

回答

5

你的代码是拼接而循环。拼接元素即使不存在也可以访问。这导致未定义的元素。

您可以考虑Array#filter

var obj = [{ "index": 0, "name": "Odonnell Noble", "gender": "male", "company": "DIGIQUE", "eail": "[email protected]" }, { "index": 1, "name": "Marie Oneal", "gender": "female", "company": "CANOPOLY", "email": "[email protected]" }, { "index": 2, "name": "Adrienne Marsh", "gender": "female", "company": "XOGGLE", "email": "[email protected]" }, { "index": 3, "name": "Goff Mullins", "gender": "male", "company": "ENDIPIN", "email": "[email protected]" }, { "index": 4, "name": "Lucile Finley", "gender": "female", "company": "AQUASSEUR", "email": "[email protected]" }, { "index": 5, "name": "Pitts Mcpherson", "gender": "male", "company": "QUARX", "email": "[email protected]" }]; 
 

 
obj = obj.filter(function (user, index) { 
 
    return (user.index !== 5 && user.index !== 2); 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');

+0

为什么他的代码不工作? Plz解释说。我认为反向while循环可能是解决它的好选择。 –

+0

谢谢,它像一个魅力:) – gespinha

1

Array#forEach

通过的forEach处理的元素的范围()在第一 调用回调的之前设置。在对forEach()的调用开始后 后追加到数组的元素将不会被回调访问。如果数组中现有元素的 值发生更改,则传递给 的值将是forEach()访问它们时的值; 被访问前被删除的元素未被访问

obj.forEach(function(user, index){ 
    var userName = user.name; 
    //console.log(index, userName); 

    if(user.index === 5 || user.index === 2){ 
    this.splice(index, 1); 

    } 
}.bind(obj)); 

这里是工作fiddle

0

的forEach被而是意味着所谓的副作用。

你的代码存在的问题是,你在迭代它时正在改变数组。因此,如果您删除一个项目,则阵列的所有其他索引将立即重新分配。这就是为什么删除一个项目后,进一步删除不会做所需的事情(在所需的位置)。

因此forEach对于影响实际数组之外的事物是很好的,这是迭代的。

这对于一个称为过滤器的函数来说是一个完美的用例,因为实际上,您对列表做了什么:你想过滤掉一些项目。

array = array.filter(function(item, index) { 
    return (index !== 5 && index !== 2) 
} 

过滤函数除了函数作为参数,其本身将被调用的数组中的每个项目。如果该函数对某个项目返回true,则会保留 - 否则将被删除。这就是为什么逻辑表达式必须在这里稍微改变的原因:它看起来像这样:保持不是索引5而不是索引2的项目。这些真或假返回函数称为谓词。

如果您想过滤掉更多的索引,该怎么办?使用locical运算符的表达式变得很快。

您可以在索引列表上使用数组方法indexOf,每次将数组的当前索引传递给索引。如果它不在里面,它会返回一个位置或-1。在后面的情况下,您希望将该项目保留在数组中。

array = array.filter(function(item, current_index) { 
    return ([2, 5].indexOf(current_index) === -1) 
} 

此外,你可以包装在一个函数:

function removeIndices(array, indices) { 
    return array.filter(function(item, current_index) { 
     return (indices.indexOf(current_index) === -1) 
    }) 
} 

最后:

array = removeIndices(array, [2, 5]);