简体   繁体   English

使用拼接删除数组中的项目

[英]Using splice to delete item in array

Why does the remaining in original array = [1, 3, 5, 7, 9]为什么原始数组中的剩余部分 = [1, 3, 5, 7, 9]

Since arr.splice(i, 1) = i is the target index and 1 is the number of item to be removed, i is increases to 10 respectively from i++ which short for i = i + 1, So why does it remove 5 index and remain 5 in the array ?由于 arr.splice(i, 1) = i 是目标索引,1 是要删除的项数,因此 i 分别从 i++ 增加到 10,即 i = i + 1 的缩写,那么为什么要删除 5 个索引并在数组中保留 5 ? that's what i know so far and i have struggled to read the docs but still have no idea to understand, please explain it for me这就是我到目前为止所知道的,我一直在努力阅读文档,但仍然不知道要理解,请为我解释一下

let arr = [1,2,3,4,5,6,7,8,9,10];

for(let i = 1; i < arr.length; i++) {
    arr.splice(i, 1);

}

It is because the length of arr decreases everytime splice function runs.这是因为每次 splice 函数运行时,arr 的长度都会减少。 Here is how the array changes.这是数组的变化方式。

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 9, 10]
[2, 4, 6, 8, 10]

So every loop, i increases and arr.length decreases by 1. so only 5 loops runs and the result is [2, 4, 6, 8, 10]所以每个循环,i 增加,arr.length 减少 1。所以只有 5 个循环运行,结果是 [2, 4, 6, 8, 10]

You're wondering why it's removing 1, 3, 5, 7, and 9, right?您想知道为什么要删除 1、3、5、7 和 9,对吗?

Here's why.这是为什么。 As the for loop iterates, i keeps increasing by one.随着 for 循环的迭代, i不断增加 1。 HOWEVER, by calling .splice , you are removing the first element of the array, so as a result, every other element moves down an index.但是,通过调用.splice ,您正在删除数组的第一个元素,因此,每个其他元素都会向下移动一个索引。

Let's play this out step by step for a few iterations of the for loop.让我们在 for 循环的几次迭代中一步一步地进行。

i = 0;我 = 0; arr.splice(0, 1) removes 1, so arr is [2, 3, 4, 5, 6, 7, 8, 9, 10] arr.splice(0, 1)移除 1,所以arr[2, 3, 4, 5, 6, 7, 8, 9, 10]

i = 1;我 = 1; arr.splice(1, 1) removes 3 , not 2, because now 3 is at index 1 of arr . arr.splice(1, 1)删除3 ,而不是 2 ,因为现在 3 位于arr索引 1 处。 Performing the splice leaves arr as [2, 4, 5, 6, 7, 8, 9, 10] .执行拼接使arr[2, 4, 5, 6, 7, 8, 9, 10]

i = 2;我 = 2; arr.splice(2, 1) removes 5, because 5 is currently at index 2. As a result, arr becomes [2, 4, 6, 7, 8, 9, 10] . arr.splice(2, 1)删除 5,因为 5 当前位于索引 2 处。因此, arr变为[2, 4, 6, 7, 8, 9, 10]

Is it clear now what's going on?现在清楚是怎么回事了吗?

If your goal is to successively remove each element, one at a time, then instead of calling .splice(i, 1) in each iteration of the loop, you should call .splice(0, 1) , since the value at index 0 changes each time you call .splice .如果您的目标是连续删除每个元素,一次一个,那么您应该调用.splice(0, 1)而不是在循环的每次迭代中调用.splice(i, 1) .splice(0, 1) ,因为索引 0 处的值每次调用.splice时都会发生变化。

You need to check the for loop end condition, i is not increasing to 10. Why?您需要检查 for 循环结束条件,i 没有增加到 10。为什么? because i < arr.length.因为我 < arr.length。 So it will like this :所以它会像这样:

Iteration 1:迭代 1:

i=0; arr.length = 10; arr = [1,2,3,4,5,6,7,8,9,10]; ==> result [2,3,4,5,6,7,8,9,10]; 

Iteration 2:迭代 2:

i=1; arr.length = 9; arr = [2,3,4,5,6,7,8,9,10];  ==> result [2,4,5,6,7,8,9,10]; 

Iteration 3:迭代 3:

i=2; arr.length = 8; arr = [2,4,5,6,7,8,9,10];  ==> result [2,4,6,7,8,9,10]; 

. . . . .and so forth .等等

i = 5 ==> arr.length: 5 ==> final result : [2, 4, 6, 8, 10]

So if you want to delete all items using splice:因此,如果您想使用 splice 删除所有项目:

let arr = [1,2,3,4,5,6,7,8,9,10];

while(arr.length > 0) {

    arr.splice(0, 1);

}

Remember, arrays are 0 based.请记住,数组是基于 0 的。 Second, the length is changing each time it evaluates.其次,每次求值时长度都在变化。

MDN links: MDN 链接:

Splice 拼接

Map 地图

So you may want to try所以你可能想尝试

i =< length

Where length is determined and is set ahead of time and constant.其中长度已确定并提前设置并保持不变。 You can try mapping the array to a new one, so the original array stays pure and unaffected by the splice.您可以尝试将数组映射到一个新数组,这样原始数组将保持纯净且不受拼接影响。

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

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