5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【備忘録】Javascriptのshift, slice, spliceの違い

Posted at

#shift
shiftを使った配列から1要素目を削除して、戻り値としてその要素を返す。
元配列への影響:有

let a = [1,2,3];
let b = a.shift();  //a=[2,3] b=[1]

#slice
slice(start, [end])は
start: 取り出す先頭のインデックス
end: 取り出す最後のインデックス+1
元配列への影響:無

let a = [1,2,3];
let b = a.slice(1);  //a=[1,2,3] b=[1]
let b = a.slice(0,1);  //a=[1,2,3] b=[1]
let b = a.slice(1,2);  //a=[1,2,3] b=[2]

#splice
slice(start, number)は 配列の一部を削除する。
(実際には置換や挿入もできる。ここでは比較のため削除のみ)
start: 取り出す先頭のインデックス
number: 取り出す要素数
元配列への影響:有

let a = [1,2,3];
let b = a.splice(0,1);  //a=[2,3] b=[1]
let b = a.splice(1,2);  //a=[1] b=[2,3]
5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?