JavaScriptで配列の要素だけを配列に格納したい(スプレッド構文)
JavaScriptで、配列に他の配列の要素だけを格納したい場合
だめなケース
'use strict'; { const otherScores = [50, 60]; const scores = [10, 20, 30, 40, otherScores]; console.log(scores); } // 実行結果 [ 10, 20, 30, 40, [ 50, 60 ] ]
あれ、配列の中に配列が入ってしまった...
要素だけをいれたいのに...
どうしよう...
困ったな...
スプレッド構文を使え...
'use strict'; { const otherScores = [50, 60]; const scores = [10, 20, 30, 40, ...otherScores]; console.log(scores); } // 実行結果 [ 10, 20, 30, 40, 50, 60 ]
できた...!!p(^^)q
スプレッド構文を使えばいいのね...
developer.mozilla.org