如何在Javascript中删除数组中的每个第二和第三个元素?

时间:2022-12-12 21:27:23

I want to delete every second and third element from an array in Javascript.

我想在Javascript中删除数组中的每个第二和第三个元素。

My array looks like this:

我的数组看起来像这样:

var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10"];

Now I want to delete every second and third element. The result would look like this:

现在我想删除每个第二和第三个元素。结果如下所示:

["Banana", "Orange", "Apple"]

I tried to use a for-loop and splice:

我试着使用for-loop和splice:

for (var i = 0; fruits.length; i = i+3) {
    fruits.splice(i+1,0);
    fruits.splice(i+2,0);
};

Of course this returns an empty array because the elements are removed while the loop is still executed. How can I do this correctly?

当然这会返回一个空数组,因为在循环仍然执行时会删除元素。我该怎么做才能正确?

Thank you.

谢谢。

6 个解决方案

#1


12  

You could approach this from a different angle and push() the value you don't want deleted into another Array:

您可以从不同的角度处理此问题,并将您不希望删除的值推送()到另一个数组中:

var firstFruits = []

for (var i = 0; i < fruits.length; i = i+3) {
    firstFruits.push(fruits[i]);
};

This approach may not be as terse as using splice(), but I think you see gain in terms of readability.

这种方法可能不像使用splice()那样简洁,但我认为您在可读性方面看到了收益。

#2


9  

This works for me.

这对我有用。

var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10","Pear","something","else"];

for(var i = 0; i < fruits.length; i++) {
    fruits.splice(i+1,2);
}

//fruits = Banana,Orange,Apple,Pear

Here's a demo that illustrates it a little better: http://jsfiddle.net/RaRR7/

这是一个演示,它更好地说明了它:http://jsfiddle.net/RaRR7/

#3


1  

Try looping through the array in reverse order

尝试以相反的顺序循环遍历数组

#4


1  

You could use filter:

你可以使用过滤器:

var filtered = [
   "Banana", 
   "yellow", 
   "23", 
   "Orange", 
   "orange", 
   "12", 
   "Apple", 
   "green", 
   "10"
].filter(function(_, i) {
    return i % 3 === 0;
})

Returns:

返回:

["Banana", "Orange", "Apple"]

#5


0  

Have you considered just copying the first, fourth, and seventh elements to a new array? It isn't very memory efficient but it'll still work fine.

您是否考虑过将第一,第四和第七个元素复制到新阵列?它的内存效率不是很高,但它仍能正常工作。

#6


0  

You'll want to move through the array backwards, then if i % 2 == 0 || i%3 == 0 then splice the element from the array

你想要向后移动数组,然后如果i%2 == 0 || i%3 == 0然后从数组中拼接元素

#1


12  

You could approach this from a different angle and push() the value you don't want deleted into another Array:

您可以从不同的角度处理此问题,并将您不希望删除的值推送()到另一个数组中:

var firstFruits = []

for (var i = 0; i < fruits.length; i = i+3) {
    firstFruits.push(fruits[i]);
};

This approach may not be as terse as using splice(), but I think you see gain in terms of readability.

这种方法可能不像使用splice()那样简洁,但我认为您在可读性方面看到了收益。

#2


9  

This works for me.

这对我有用。

var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10","Pear","something","else"];

for(var i = 0; i < fruits.length; i++) {
    fruits.splice(i+1,2);
}

//fruits = Banana,Orange,Apple,Pear

Here's a demo that illustrates it a little better: http://jsfiddle.net/RaRR7/

这是一个演示,它更好地说明了它:http://jsfiddle.net/RaRR7/

#3


1  

Try looping through the array in reverse order

尝试以相反的顺序循环遍历数组

#4


1  

You could use filter:

你可以使用过滤器:

var filtered = [
   "Banana", 
   "yellow", 
   "23", 
   "Orange", 
   "orange", 
   "12", 
   "Apple", 
   "green", 
   "10"
].filter(function(_, i) {
    return i % 3 === 0;
})

Returns:

返回:

["Banana", "Orange", "Apple"]

#5


0  

Have you considered just copying the first, fourth, and seventh elements to a new array? It isn't very memory efficient but it'll still work fine.

您是否考虑过将第一,第四和第七个元素复制到新阵列?它的内存效率不是很高,但它仍能正常工作。

#6


0  

You'll want to move through the array backwards, then if i % 2 == 0 || i%3 == 0 then splice the element from the array

你想要向后移动数组,然后如果i%2 == 0 || i%3 == 0然后从数组中拼接元素