简体   繁体   English

Array.splice在Firefox控制台中不起作用

[英]Array.splice not working in firefox console

Trying the following code from the MDN site 从MDN站点尝试以下代码

var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];

// removes 0 elements from index 2, and inserts 'drum'
var removed = myFish.splice(2, 0, 'drum');

Doesn't assign to remove the expected output of splice ['angel', 'clown', 'drum', 'mandarin', 'surgeon'] , but instead and empty array [] . 不指定删除拼接['angel', 'clown', 'drum', 'mandarin', 'surgeon']的预期输出,而是删除空数组[]

I think you are confused by the return value of splice . 我认为您对splice的返回值感到困惑。 It does not return the modified array, but the elements removed. 它不返回修改后的数组,而是删除了元素。 In this case, you removed none, so if you log removed , you get an empty Array . 在这种情况下,您没有删除任何内容,因此,如果removed日志,则会得到一个空Array

var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];
var removed = myFish.splice(2, 0, 'drum');
console.log(myFish);
console.log(removed);

Output: 输出:

Array [ "angel", "clown", "drum", "mandarin", "surgeon" ]
Array [  ]

Array.prototype.splice在原始数组上运行。

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

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