简体   繁体   English

与 javascript array.splice() 混淆

[英]Confusion with javascript array.splice()

I'm really confused about this.我真的很困惑。

My understanding was that array.splice(startIndex, deleteLength, insertThing) would insert insertThing into the result of splice() at startIndex and delete deleteLength 's worth of entries?我的理解是array.splice(startIndex, deleteLength, insertThing)会在startIndex处将insertThing插入splice()的结果并删除deleteLength的条目? ... so: ... 所以:

var a = [1,2,3,4,5];
var b = a.splice(1, 0, 'foo');
console.log(b);

Should give me:应该给我:

[1,'foo',2,3,4,5]

And

console.log([1,2,3,4,5].splice(2, 0, 'foo'));

should give me应该给我

[1,2,'foo',3,4,5]

etc.等等。

But for some reason it's giving me just an empty array?但出于某种原因,它只给了我一个空数组? Take a look: http://jsfiddle.net/trolleymusic/STmbp/3/看一看: http : //jsfiddle.net/trolleymusic/STmbp/3/

Thanks :)谢谢 :)

The "splice()" function returns not the affected array, but the array of removed elements. “splice()”函数返回的不是受影响的数组,而是已删除元素的数组。 If you remove nothing, the result array is empty.如果不删除任何内容,则结果数组为空。

splice() modifies the source array and returns an array of the removed items. splice()修改源数组并返回已删除项的数组。 Since you didn't ask to remove any items, you get an empty array back.由于您没有要求删除任何项目,因此您会返回一个空数组。 It modifies the original array to insert your new items.它修改原始数组以插入您的新项目。 Did you look in a to see what it was?你有没有看一看它是什么? Look for your result in a .寻找你的结果在a

var a = [1,2,3,4,5];
var b = a.splice(1, 0, 'foo');
console.log(a);   // [1,'foo',2,3,4,5]
console.log(b);   // []

In a derivation of your jsFiddle, see the result in a here: http://jsfiddle.net/jfriend00/9cHre/ .在你的jsfiddle的推导,看到的结果是在a位置: http://jsfiddle.net/jfriend00/9cHre/

The array.splice function splices an array returns the elements that were removed . array.splice函数拼接一个数组返回被删除的元素 Since you are not removing anything and just using it to insert an element, it will return the empty array.由于您没有删除任何内容而只是使用它来插入元素,因此它将返回空数组。

I think this is what you are wanting.我想这就是你想要的。

var a = [1,2,3,4,5]; 
a.splice(1, 0, 'foo'); 
var b = a;
console.log(b); 

I had this issue.我有这个问题。 "The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place." “ splice() 方法通过删除或替换现有元素和/或在适当位置添加新元素来更改数组的内容。” https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

This will work:这将起作用:

var a = [1,2,3,4,5];
a.splice(1,0,'Foo');
console.log(a);

If you want it inside a function you could do this:如果你想在一个函数中使用它,你可以这样做:

function placeInMiddle(arr){
     
     arr.splice(2,0, "foo");
      return arr;
    
}  
placeInMiddle([1,2,6,7])

Array.prototype.splice() page on Mozilla developer is more clear than other resources about how the splice method works and how it can be implemented, described briefly below! Mozilla 开发人员的 Array.prototype.splice() 页面比其他资源更清楚地了解 splice 方法的工作原理及其实现方式,下面简要介绍!

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

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

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