Attention the return value;

let data=['a','b','c','d','e'];
//splice NO chain array; return value NO self.
//
//data.splice(0,0,'f'); //unshift//f a b c d e
//data.splice(data.length,0,'f');//same push// a b c d e f

//if(~data.indexOf('c'))
//data.splice(data.indexOf('c'),1,'f');//replace c->f//a b f d e

//if(~data.indexOf('c'))
//data.splice(data.indexOf('c'),0,'f');//before push//a b f c d e

//if(~data.indexOf('c'))
//data.splice(data.indexOf('c')+1,0,'f');//after push//a b c f d e
;
data.splice(data.indexOf('c'),1);//remove me
console.log(data)

[].splice(target_number,change_count,target_data)

spliceは破壊的なので、データ自体が更新対象の場合に多用する機会が多い。splice一つでおおよその配列操作を完了させることが出来る。

<script type="text/plain" name="cheat-sheet">
let data=['a','b','c','d','e'];
//splice NO chain array; return value NO self.
//[].splice(target_number,change_count,target_data)
//data.splice(0,0,'f'); //unshift//f a b c d e //parent.prependTo
//data.splice(data.length,0,'f');//same push// a b c d e f //parent.appendTo
//if(~data.indexOf('c')) data.splice(data.indexOf('c'),1,'f');//replace c->f//a b f d e// element.replace 
//if(~data.indexOf('c')) data.splice(data.indexOf('c'),0,'f');//before push//a b f c d e// prepend siblingsTo
//if(~data.indexOf('c')) data.splice(data.indexOf('c')+1,0,'f');//after push//a b c f d e// siblingsTo
//if(~data.indexOf('c'))
data.splice(data.indexOf('c'),1);//remove me
console.log(data)
</script>
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.