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の分割代入

Posted at

こんなデータを受け取ったとして

const data = {
   name: 'hoge',
   age: '25',
   public: true
}

こうすると

const { name, age, public } = data

dataオブジェクトのプロパティを取り出して、別個の変数に代入することができる
※配列もできる

console.log(name)
// hoge

変数名とオブジェクトのキーの値は同じ必要がある

でも変数は別名にしたい時はこうする


const { name, text, public: publicKey } = data

jsの予約語と同名のキーでも、これで安心:metal:

console.log(publicKey)
// true
0
0
1

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?