遍历数组并删除元素时的循环处理

9
我想知道在循环内部迭代长度会改变的数组的可靠和/或标准方法是什么。我之所以问这个问题,是因为每次我想做这件事时,我都会选择不同的方法,例如:
for ( var i = 0; i < myarray.length; i++ ) {
  if (myarray[i] === 'something') {
    myarray.splice(i, 1);

    // *to avoid jumping over an element whose index was just shifted back to the current i
    i--;
  }
}

或者

var i = 0;
while (myarray[i]) {
  if (myarray[i] === 'something') {
    myarray.splice(i, 1);
  } else {
    i++;
  }
}

以下是我通常使用的方法,但我很好奇是否有标准的做法。


你查看过这个回答吗?[https://dev59.com/Smkw5IYBdhLWcg3wdKQv] - Rui Marques
1
你可以在第一个例子中保留正向迭代,并将后缀递减的 i 直接放入 .splice() 调用中:myarray.splice(i--, 1); - user2437417
2个回答

23

我发现在另一个方向上迭代更简单:

for (var i=myarray.length; i--; ) {
   if (myarray[i] === 'something') myarray.splice(i, 1);
}

这样你在删除时就不必改变增量。

许多开发人员,尤其是那些之前没有接触过类 C 语言的 JavaScript 开发人员,会觉得处理递减运算符的微妙之处很困惑。我编写的循环也可以写成:

for (var i=myarray.length-1; i>=0; i--) {

+1 今天早上我在面试中问了一个类似的问题;-) 最好的部分是,即使我自己没有想到这个解决方案。 - Atif
这绝对是迭代通过将有项目删除的集合的推荐方式。我在任何语言中都这样做。 - Jeff LaFay
@jlafay,我取消了你的编辑,因为 for (var i = myarray.length; i > 0; i--) 不是同样的操作(它从数组外部开始并且会漏掉索引为0的元素)。 - Denys Séguret
你两点都说对了,但这也意味着设置imyarray.length的代码也是错误的。应该改为var i = myarray.length - 1。在for循环条件中递减索引有点不太好,这就是我更改的原因。我应该检查i >= 0 - Jeff LaFay
我喜欢你的第一种方法 - 它只会在 i === 0 时失败,这是一个 falsy 值;但它会在 i 被评估后递减,所以无论如何都会到达 0。 - keldar
显示剩余6条评论

0
无论你选择哪种方式,从后往前数是最简单的。这也取决于你的数组是否稀疏以及你是否希望它保持稀疏。最简单的方法是创建一个可重用的函数和自己的库。你可以这样做。如果你将compress设置为true,那么你的数组将变成连续的而不是稀疏的数组。这个函数将删除所有匹配的值,并返回一个包含已删除元素的数组。

Javascript

function is(x, y) {
    if (x === y) {
        if (x === 0) {
            return 1 / x === 1 / y;
        }

        return true;
    }

    var x1 = x,
        y1 = y;

    return x !== x1 && y !== y1;
}

function removeMatching(array, value /*, compress (default = false)*/ ) {
    var removed = [],
        compress = arguments[2],
        index,
        temp,
        length;

    if (typeof compress !== "boolean") {
        compress = false;
    }

    if (compress) {
        temp = [];
        length = array.length;
        index = 0;
        while (index < length) {
            if (array.hasOwnProperty(index)) {
                temp.push(array[index]);
            }

            index += 1;
        }
    } else {
        temp = array;
    }

    index = 0;
    length = temp.length;
    while (index < length) {
        if (temp.hasOwnProperty(index) && is(temp[index], value)) {
            if (compress) {
                removed.push(temp.splice(index, 1)[0]);
            } else {
                removed.push(temp[index]);
                delete temp[index];
            }
        }

        index += 1;
    }

    if (compress) {
        array.length = 0;
        index = 0;
        length = temp.length;
        while (index < length) {
            if (temp.hasOwnProperty(index)) {
                array.push(temp[index]);
            }

            index += 1;
        }
    }

    return removed;
}

var test = [];

test[1] = 1;
test[50] = 2;
test[100] = NaN;
test[101] = NaN;
test[102] = NaN;
test[200] = null;
test[300] = undefined;
test[400] = Infinity;
test[450] = NaN;
test[500] = -Infinity;
test[1000] = 3;

console.log(test);
console.log(removeMatching(test, NaN));
console.log(test);
console.log(removeMatching(test, Infinity, true));
console.log(test);

输出

[1: 1, 50: 2, 100: NaN, 101: NaN, 102: NaN, 200: null, 300: undefined, 400: Infinity, 450: NaN, 500: -Infinity, 1000: 3]
[NaN, NaN, NaN, NaN]
[1: 1, 50: 2, 200: null, 300: undefined, 400: Infinity, 500: -Infinity, 1000: 3]
[Infinity]
[1, 2, null, undefined, -Infinity, 3] 

jsfiddle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接