LoginSignup
0
0

More than 1 year has passed since last update.

Object.keysの使い方

Last updated at Posted at 2022-05-20

Object.keysとは?

  • あるオブジェクトがあったとして、その中のキー名だけを抽出したい時に使う
  • 値が取れたあとは新しい配列を生成する

実践

まずはじめに以下のようなオブジェクトを用意します。

index.js
const datas = {id: 1, string: 'box', number: 10, boolean: true}

次にidstringnumerbooleanを抽出します。(本題)

index.js
const dataKey = Object.keys(data)
console.log(dataKey)
// ['id', 'string', 'numer', 'boolean']

これですべてのキーを取ることができました。

コンソールで実際に試すと以下のようになります。
スクリーンショット 2022-05-20 23.12.01.png

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