如何遍历数组并删除 JavaScript 中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16352546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate over an array and remove elements in JavaScript
提问by Captain Stack
I have an array of elements and need to remove certain ones from it. The problem is that JavaScript doesn't seem to have a for each loop and if I use a for loop I run into problems with it basically trying to check elements beyond the bounds of the array, or missing elements in the array because the indexes change. Let me show you what I mean:
我有一组元素,需要从中删除某些元素。问题是 JavaScript 似乎没有 for each 循环,如果我使用 for 循环,我会遇到问题,它基本上是尝试检查超出数组边界的元素,或者由于索引更改而导致数组中缺少元素. 让我告诉你我的意思:
var elements = [1, 5, 5, 3, 5, 2, 4];
for(var i = 0; i < elements.length; i++){
if(elements[i] == 5){
elements.splice(i, 1);
}
}
The problem is that when elements[1] is removed, elements[2] becomes elements[1]. So first problem is that some elements are never examined. The other problem is that .length changes and if I hard code the bounds, then I might be trying to examine elements beyond the bounds of the array. So what's the best way to do this incredibly simple thing?
问题是当元素[1] 被移除时,元素[2] 变成了元素[1]。所以第一个问题是某些元素从未被检查过。另一个问题是 .length 会发生变化,如果我对边界进行硬编码,那么我可能会尝试检查超出数组边界的元素。那么做这件非常简单的事情的最佳方法是什么?
回答by Jean-Bernard Pellerin
Start from the top!
从顶部开始!
var elements = [1, 5, 5, 3, 5, 2, 4];
for(var i = elements.length -1; i >= 0 ; i--){
if(elements[i] == 5){
elements.splice(i, 1);
}
}
回答by KooiInc
You could use the filter
method here:
您可以在filter
此处使用该方法:
var elements = [1, 5, 5, 3, 5, 2, 4].filter(function(a){return a !== 5;});
//=> elements now [1,3,2,4]
Or if you don't want to touch elements
:
或者,如果您不想触摸elements
:
var elementsfiltered
,elements = [1, 5, 5, 3, 5, 2, 4]
.filter( function(a){if (a!==5) this.push(a); return true;},
elementsfiltered = [] );
//=> elementsfiltered = [1,3,2,4], elements = [1, 5, 5, 3, 5, 2, 4]
See MDN documentationfor filter
见MDN文档的filter
Alternatively you can extend the Array.prototype
或者,您可以扩展 Array.prototype
Array.prototype.remove = Array.prototype.remove || function(val){
var i = this.length;
while(i--){
if (this[i] === val){
this.splice(i,1);
}
}
};
var elements = [1, 5, 5, 3, 5, 2, 4];
elements.remove(5);
//=> elements now [1,3,2,4]
回答by rouble
var elements = [1, 5, 5, 3, 5, 2, 4];
var i = elements.length;
while (i--) {
if (elements[i] == 5) {
elements.splice(i, 1);
}
}
console.log(elements);
回答by Geert
Using Array.shift():
var array = [1, 2, 3, 'a', 'b', 'c'];
while (array.length > 0) {
console.log(array.shift());
}
Edit: Probably does not suit the specs. I misread the question (only remove certainelements) and was too eager instead to add a method that was not mentioned yet...
编辑:可能不适合规格。我误读了这个问题(只删除了某些元素)并且太急于添加一个尚未提到的方法......
回答by Lachmanski
You could simply decrement i
whenever you remove an item.
您可以i
在删除项目时简单地递减。
var elements = [1, 5, 5, 3, 5, 2, 4];
var l = elements.length;
for(var i = 0; i < l; i++){
if(elements[i] == 5){
elements.splice(i, 1);
i--;
}
}
console.log(elements);
回答by Xotic750
This is an example of using Array.indexOf, whileand Array.spliceto remove elements inline.
这是使用Array.indexOf、while和Array.splice删除内联元素的示例。
var elements = [1, 5, 5, 3, 5, 2, 4];
var remove = 5;
var index = elements.indexOf(remove);
while (index !== -1) {
elements.splice(index, 1);
index = elements.indexOf(remove);
}
console.log(elements);
On jsfiddle