LoginSignup
0
0

More than 5 years have passed since last update.

Array splice same function

Last updated at Posted at 2018-04-01

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>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0