js .splice()

var removed = myFish.splice([i], 削除する数, '挿入する文字列');

 

 

凡庸性の高い.splice()。復習

developer.mozilla.org

[2]に要素を一つ追加する

var myFish = ['angel', 'clown', 'mandarin', 'drum','sturgeon']; 
var removed = myFish.splice(2, 0, 'drum');
//myFishは["angel", "clown", "drum", "mandarin", "sturgeon"]

[3]から一つ取り除く

var myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'];
var removed = myFish.splice(3, 1);

// myFish は ["angel", "clown", "drum",'mandarin', "sturgeon"]
// removed は ["mandarin"]

[2]から一つ取り除き、そこへ一つ挿入する

var myFish = ['angel', 'clown', 'drum', 'trumpet','sturgeon']; 
var removed = myFish.splice(2, 1, 'trumpet');

// myFish は ["angel", "clown", "trumpet", "sturgeon"] // removed は ["drum"]