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

More than 1 year has passed since last update.

分割代入の基本をまとめておく

0
Posted at

分割代入とは

分割代入とは、オブジェクトや配列の中身を一発で取り出して、変数に代入できる便利な書き方。
代入した変数はそのまま使うことができる!また、別名もつけられる!

基本的な書き方

通常の書き方はこんな感じで面倒臭い。

const user = { name: 'Alice', age: 25 };

const name = user.name;
const age = user.age;

分割代入を使えば、以下の1行だけで、nameとageが使えるようになる!

const { name, age } = user;

配列の場合も同様。

const colors = ['red', 'green', 'blue'];
const [first, second] = colors;

console.log(first);  // 'red'
console.log(second); // 'green'

別名をつけることも可能!

分割代入を行う際に、{変数名: 別名} = オブジェクト名とすると、別名で扱うことも可能となる。下記の場合はfldYmyearMonthとして使用することができる。

const { fldrYm: yearMonth } = folder;
console.log(yearMonth); // '202508'

まとめ

分割代入を使うと、オブジェクトの中身を一瞬で代入して使用できるようになる!
久々にReactのプロジェクトを触ったので、ほぼ忘れていましたが、こうやってアウトプットして思い出していきます!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?