简体   繁体   English

从数组中删除一系列元素

[英]Remove a range of elements from an array

I want to remove a range of elements from an array :我想从array中删除一系列元素:

 var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"]; var a = fruits.indexOf("Apple"); var b = fruits.indexOf("Mango"); var removedCars = fruits.splice(a, b); console.log(fruits);

So I am expecting:所以我期待:

["Banana", "Orange1", "Bananax", "Orangex"]

But the result is:但结果是:

["Banana", "Orange1", "Orangex"]

Why is this happening?为什么会这样?

Are there any faster and better ways of doing this?有没有更快更好的方法来做到这一点?

The second param of Array.prototype.splice() method is the number of elements to be removed and not an ending index. Array.prototype.splice()方法的第二个参数是要删除的元素数量,而不是结束索引。

You can see from the Array.prototype.splice() MDN Reference that:您可以从Array.prototype.splice() MDN 参考中看到:

Parameters参数

start Index at which to start changing the array (with origin 0). start开始更改数组的索引(原点为 0)。 If greater than the length of the array, actual starting index will be set to the length of the array.如果大于数组的长度,实际起始索引将设置为数组的长度。 If negative, will begin that many elements from the end of the array (with origin 1) and will be set to 0 if absolute value is greater than the length of the array.如果为负数,将从数组末尾开始很多元素(原点为 1),如果绝对值大于数组的长度,则设置为 0。

deleteCount Optional An integer indicating the number of old array elements to remove. deleteCount可选 一个整数,指示要删除的旧数组元素的数量。 If deleteCount is 0, no elements are removed.如果 deleteCount 为 0,则不删除任何元素。 In this case, you should specify at least one new element.在这种情况下,您应该至少指定一个新元素。 If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.如果 deleteCount 大于从 start 开始数组中剩余的元素数,则将删除数组末尾的所有元素。

Solution:解决方案:

You need to calculate the number of elements between these two indexes, so use b-a+1 to get the correct count.你需要计算这两个索引之间的元素个数,所以使用b-a+1得到正确的计数。

Demo:演示:

This is how should be your code:这应该是你的代码:

 var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"]; var a = fruits.indexOf("Apple"); var b = fruits.indexOf("Mango"); var removedFruits = fruits.splice(a, b-a+1); console.log(fruits);

Here's one way to do it with filter:这是使用过滤器的一种方法:

var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"];
var a = fruits.indexOf("Apple");
var b = fruits.indexOf("Mango");

//returns items not equal to a or b
function fruitfilter(item, index){
return index !== a && index !== b;
}

//apply the filter to the array, returns a new array
var newfruits = fruits.filter(fruitfilter);
 //log the new fruits
console.log(newfruits);

Here's a jsfiddle: link这是一个jsfiddle:链接

发生这种情况是因为 array.splice() 采用第一个索引并且要删除的元素数不是最后一个索引尝试

fruits.splice(a, b - a + 1);

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

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