简体   繁体   中英

Array.splice not working in firefox console

Trying the following code from the MDN site

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 [] .

I think you are confused by the return value of 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 .

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在原始数组上运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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