1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javascriptのデストラクチャリング(配列)

1
Last updated at Posted at 2026-02-27

基本構文

const [変数、変数、変数] = arr

このようなフルーツの配列があったとして

const fruits = ["Apple", "Banana", "Orange", "kiwy", "Peach", "pineApple"];

Apple,Banana,Orangeを取得したかったら

const fruits = ["Apple",
"Banana", "Orange", "kiwy", "Peach", "pineApple"];

let fruit1 = fruits[0]; /*Apple*/
let fruit2 = fruits[1]; /*Banana*/
let fruit3 = fruits[2]; /*Orange*/
console.log(fruit1, fruit2, fruit3); /*Apple, Banana, Orange*/

というようにそれぞれに対して変数を宣言し
インデックスを指定した文を書かないといけなかったけど
デストラクチャリングなら

const fruits = ["Apple", "Banana", "Orange", "kiwy", "Peach", "pineApple"];

const [fruit1, fruit2, fruit3] = frutis
console.log(fruit1, fruit2, fruit3); /*Apple, Banana, Orange*/

というふうに
インデックスを指定しなくても[fruit1, fruit2, fruit3] の順に
インデックス0のものから代入できる。

また要素を飛ばして取得したい場合は

const fruits = ["Apple", "Banana", "Orange", "kiwy", "Peach", "pineApple"];

const [fruit1, ,fruit3] = frutis 
console.log(fruit1,fruit3) /*Apple, Orange*/

というようにカンマで区切って要素を飛ばすことができる。

ネストされた配列

const fruits = ["Apple", "Banana", "Orange", ["kiwy", "Peach", "pineApple"]];

const [fruit1, fruit2, fruit3, [fruit4, fruit5, fruit6]] = fruits;
console.log(fruit4, fruit5, fruit6);  /*kiwy Peach pineApple */

宣言するときにネストさせてあげれば代入できる。

次はオブジェクトのデストラクチャリングを学ぶので

次回はそのアウトプットを行おうと思います!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?