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 3 years have passed since last update.

【JavaScript】分割代入

Last updated at Posted at 2020-05-17

オブジェクトのプロパティを変数に代入することができる。

main.js
let menu = {
    title: 'スパゲッティポリタン',
    cook: 'メインシェフ',
    price: 1500,
    resources: {
        pasta: '生パスタ',
        main: 'ベーコン'
    }
};
console.log(menu.title);
console.log(menu.cook);
console.log(menu.resources.pasta);
console.log(menu.resources.main);

let { title, cook, resources: { pasta, main, topping } } = menu;

console.log(title);
console.log(cook);
console.log(pasta);
console.log(main);
console.log(topping);
スパゲッティポリタン
メインシェフ
生パスタ
ベーコン

スパゲッティポリタン
メインシェフ
生パスタ
ベーコン
undefined

別の変数名に割り当てることも可能

main.js
let { title: name, cook: partner, resources: { pasta: kind, main, topping } } = menu;


console.log(name);
console.log(partner);
console.log(kind);
console.log(main);
console.log(topping);

スパゲッティポリタン
メインシェフ
生パスタ
ベーコン
undefined
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?