简体   繁体   English

在javascript中从数组中删除项目

[英]Removing items from an array in javascript

This code is supposed to remove numbers from an array.此代码应该从数组中删除数字。 The expected output of the code below should be the strings "cat", "dog", and "bird" logged to the console.下面代码的预期输出应该是记录到控制台的字符串“cat”、“dog”和“bird”。 The actual output is actually leaves the console blank.实际输出实际上将控制台留空。 Note that for the website I am programming this on, removeItem() is the correct notation for removing an array item.请注意,对于我正在对其进行编程的网站, removeItem()是删除数组项的正确表示法。

var animals = ["cat", "dog", 2, 11, "bird", 13];
var i;

for (i = 0; ((isNaN(animals[i])) == false) && (i < animals.length - 1); i++) {
  removeItem(animals, i);
  console.log(animals);
}

The problem is the second argument to for needs to be the MAX value for the loop not what you have.问题是for的第二个参数需要是循环的 MAX 值,而不是您拥有的值。

var animals = ["cat", "dog", 2, 11, "bird", 13];
var i;

for (i = 0; i<animals.length; i++) {
  if(isNaN(animals[i])){
    removeItem(animals, i);
  }
}

console.log(animals);

for a complete replacement this will replace your need for the removeItem function also对于完全替换,这也将取代您对 removeItem 功能的需求

var animals = animals.filter(function(el) {
    return el.length && el==+el;
});

There are two conceptual issues here.这里有两个概念问题。 First, you have a condition on a specific value in the loop's condition.首先,您对循环条件中的特定值有一个条件。 In other words, the loop will terminate when the first number is encountered.换句话说,循环将在遇到第一个数字时终止。 Second, you're modifying the array while iterating over it, which will probably mess up your result.其次,您在迭代数组时修改了它,这可能会弄乱您的结果。

Using a filter call would address both issues and also clean up your code:使用filter调用可以解决这两个问题并清理你的代码:

animals = animals.filter(x => isNaN(x));

The for loop should look something like this: for 循环应该是这样的:

for (i = 0; i < animals.length; i++) {
    // check if isNaN inside here then remove..
}

But I would suggest using filter() instead.但我建议改用 filter() 。

var animals = ['cat', 'dog', 2, 11, 'bird', 13];
var result = animals.filter(item => isNaN(item));
console.log(result);

The reason the loop never runs is because the first element is not a number循环永远不会运行的原因是因为第一个元素不是数字

therefore the condition in the for loop fails on the first test因此 for 循环中的条件在第一次测试中失败

You need to move the test for NaN inside the loop您需要在循环内移动 NaN 的测试

Assuming removeItem(animals, i);假设removeItem(animals, i); mutates the array, you'll also have another issue改变数组,你还会有另一个问题

If you remove an item in the array, then everything to the right of the removed item shifts one to the left ... but the for loop will increase i, therefore the next element will be skipped如果删除数组中的一项,则删除项右侧的所有内容都会向左移动一个……但是 for 循环会增加 i,因此将跳过下一个元素

so, whenever you have 2 numbers in a row, the second will not be removed - ie in this case, the 11因此,每当您连续有 2 个数字时,第二个数字将不会被删除 - 即在这种情况下, 11

Ignoring the mis-placed isNaN test, you also loop until i < animals.length - 1 - which means you'd never test the last element - so in this case, the 13 will not be removed either忽略错误放置的isNaN测试,您还会循环直到i < animals.length - 1 - 这意味着您永远不会测试最后一个元素 - 所以在这种情况下,13 也不会被删除

 function removeItem(arr, i) { arr.splice(i, 1); } var animals = ["cat", "dog", 2, 11, "bird", 13]; var i; for (i = 0; i < animals.length - 1; i++) { if (!isNaN(animals[i])) { removeItem(animals, i); } } console.log(animals);

In the above, when i == 2 , the value of animals[i] is 2 ... this gets removed ... now i == 2 still, and animals[i] is 11 ... but before the next iteration of the loop occurs, i becomes 3 and the 11 value slips through the code在上面,当i == 2animals[i]值为2 ......这被删除了......现在i == 2仍然,而animals[i]11 ......但在下一次迭代之前循环发生时, i变为3并且 11 值从代码中溜走

You could simply put --i in the if condition after removeItem ... or even use removeItem(animals, i--) - that way removeItem is called with the current value of i, which then gets decreased, then increased by the i++ before the next iteration您可以简单地将--i放在removeItem之后的if条件中 ... 甚至使用removeItem(animals, i--) - 这样 removeItem 会使用 i 的当前值调用,然后减少,然后通过i++增加在下一次迭代之前

That's could look misleading to some people, but works这可能会误导某些人,但确实有效

 function removeItem(arr, i) { arr.splice(i, 1); } var animals = ["cat", "dog", 2, 11, "bird", 13]; var i; for (i = 0; i < animals.length; i++) { if (!isNaN(animals[i])) { removeItem(animals, i--); } } console.log(animals);

A cleaner and easier way to fix that is to iterate through the array from end to start一种更清晰、更简单的解决方法是从头到尾遍历数组

 function removeItem(arr, i) { arr.splice(i, 1); } var animals = ["cat", "dog", 2, 11, "bird", 13]; var i; for (i = animals.length - 1; i >= 0; --i) { if (!isNaN(animals[i])) { removeItem(animals, i); } } console.log(animals);

There is some issue that needs to be changed:有一个问题需要修改:

  • Loop backwards throught the array because you are removing items.在数组中向后循环,因为您正在删除项目。
  • Create a condition to remove desidered items.创建条件以删除所需的项目。
  • Log the result outside the loop.在循环外记录结果。

After it your code will be like this:之后你的代码将是这样的:

var animals = ["cat", "dog", 2, 11, "bird", 13];

for (var i = animals.length - 1; i >= 0; i--) {
    if (isNaN(animals[i])) { 
        removeItem(animals, i);
    }
}

console.log(animals);

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

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