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?

Object.○○()の使い方

Last updated at Posted at 2024-11-24

Object.○○()

1. Object.keys(オブジェクト名)

指定したオブジェクトの各プロパティのキーを配列で取得できる

2. Object.values(オブジェクト名)

指定したオブジェクトの各プロパティの値を配列で取得できる

3. Object.entries(オブジェクト名)

指定したオブジェクトのプロパティを配列で取得できる

let person = {
  name: 'Takuto',
  gender: '男性',
  age: 20,
};

console.log(Object.keys(person));
// ['name', 'gender', 'age']

console.log(Object.values(person));
// ['Takuto', '男性', 20]

console.log(Object.entries(person));
// [['name', 'Takuto'],['gender', '男性'],['age', 20]]

Object.○○()を使ってfor...in文をfor...of文で記述

let person = {
  name: 'Takuto',
  gender: '男性',
  age: 20,
};

//perosnの各プロパティのキーを出力
//for in の場合
for(const key in person){
  console.log(key);
}

//for of の場合
for(const value of Object.keys(person)){
  console.log(value);
}

// name
// gender
// age


//perosnの各プロパティの値を出力
//for in の場合
for(const key in person){
  console.log(person[key]);
}

//for of の場合
for(const value of Object.values(person)){
  console.log(value);
}

// Takuto
// 男性
// 20
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?