0
0

More than 1 year has passed since last update.

113.イテレーターとスプレッド演算子

Posted at

スプレッド演算子

展開した配列を新しく変数に代入したい時

書き方

1.配列を作成する
2.[]の中に...配列

配列を配列として代入したい時
const arry1 = [1,2,3,4,5];
const arry2 = [...arry1];
console.log(arry2)

>結果
スクリーンショット 2022-09-07 15.51.56.png
スプレッド演算子はイテレータの操作に従う

残余引数

仮引数をもとに実引数argsを配列として扱う

function Func(...args){
    // 引数が要素として取得されるので
    // argsは[要素1,要素2,要素3]となる
}
Func(要素1,要素2,要素3)

書き方


function Arg(...args){
  console.log(args)
}

Arg(1,2,3,4,5,99)

ポイント

*データの参照元に注意

const arry1 = [1,2,3,4,5];
const arry2 = [...arry1];
console.log(arry1)
console.log(arry2)
// 見た目は同じだけど
console.log(arry1 === arry2)

1,2は同じように見えて違うんだよ、、
*データの参照元に注意!

配列の比較には

1.文字列型に変換する
2.カスタム関数を作成する
を比較しましょう。
<参考>
https://qiita.com/ba--shi/items/46e2d42ea5fb9e94926f

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