简体   繁体   English

具有负值索引的array.splice意外行为

[英]array.splice unexpected behavior with negative values index

I noticed MDN mentioned in the description of start Parameter : 我注意到开始参数的描述中提到了MDN

  • Index at which to start changing the array (with origin 0) 开始更改数组的索引(原点为0)
  • If negative , will begin that many elements from the end of the array (with origin -1) 如果为negative,将从数组的末尾开始多个元素(原点为-1)

This description working very good for me on removing elements for me : 此说明对我来说对删除元素非常有用

Removing 1st element : 删除第一个元素:

 let foo = ["a", "b", "c"]; foo.splice(0,1) // "a" removed console.log(foo);//["b", "c"] 

Removing last element : 删除最后一个元素:

 let foo = ["a", "b", "c"]; foo.splice(-1,1) // "c" removed console.log(foo);//["a", "b"] 

Now let's Try add elements 现在让我们尝试添加元素

add element at 1st : 1st处添加元素:

 let foo = ["a", "b", "c"]; foo.splice(0,0,"x") // "x" added to 1st console.log(foo);//["x", "a", "b", "c"] 

add element to the last : 在最后添加元素

 let foo = ["a", "b", "c"]; foo.splice(-1,0,"x") // I expected to added "x" as a last element // but instead it shows ["a", "b", "x", "c"] console.log(foo); 

can anyone explain why ? 谁能解释为什么?

the last case shows ["a", "b", "x" , "c"] 最后一种情况显示[“ a”,“ b”, “ x” ,“ c”]

if you wanna remove the same element from it's position we should remove it from index of -2 not -1 : 如果您想从其位置删除相同的元素,我们应该将其从-2而不是-1的索引中删除:

 let foo = ["a", "b", "c"]; foo.splice(-1,0,"x") console.log(foo); //["a", "b", "x", "c"] foo.splice(-2,1) console.log(foo); 

Thanks... 谢谢...

Initially lets consider foo as ["a", "b", "c"] and foo.length is 3. 最初,我们将foo视为["a", "b", "c"]foo.length为3。

So when you are adding the element using foo.splice(-1,0,"x") , the index is calculated as foo.length - 1 which is 2. 因此,当您使用foo.splice(-1,0,"x")添加元素时,索引的计算方式为foo.length - 1 ,即2。

So the new element is inserted at the index 2: 因此,将新元素插入到索引2:

["a", "b", "c"]
  0    1    2
            ^
            |__________"x" added here 

So the new array becomes ["a", "b", "x", "c"] as "x" is inserted at index 2 and "c" is shifted by one index. 这样,新数组将成为["a", "b", "x", "c"]因为在索引2处插入了“ x”,并且将“ c”移位了一个索引。

When you remove the element using foo.splice(-2,0) , the index at which it is removed foo.length - 2 which is 2: 当您使用foo.splice(-2,0)删除元素时,删除元素的索引foo.length - 2为2:

["a", "b", "x", "c"]
  0    1    2    3
            ^
            |__________ "x" is removed

And the array becomes ["a", "b", "c"] . 并且数组变成["a", "b", "c"]

Splice function accepts three parameters, 拼接功能接受三个参数,

  • start : Index from where to change the array. start :从何处更改数组的索引。
  • deleteCount : Number of elements to remove. deleteCount :要删除的元素数。
  • items(varargs) : Elements to add, beginning from start index. items(varargs) :要添加的元素,从start索引start
let foo = ["a", "b", "c"];
foo.splice(-1,0,"x") 
// I expected to added "x" as a last element
// but instead it shows ["a", "b", "x", "c"]
console.log(foo);

In the above code snippet, you are passing start as -1, so it's get added on last index (which is calculated as arr.length - 1 ) and deleteCount as 0 so no elements are deleted instead they are moved to right by one index. 在上面的代码片段中,您以-1 start ,因此将其添加到最后一个索引(计算为arr.length - 1 )上,并将deleteCount设为0,因此不会删除任何元素,而是将它们向右移一个索引。

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

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