Open1
オブジェクトの反復処理
keys,values,entries
const user = {
id: 5,
name: 'Chelsey Dietrich',
username: 'Kamren',
email: 'Lucio_Hettinger@annie.ca',
};
console.log(Object.keys(user)); // ['id', 'name', 'username', 'email']
console.log(Object.values(user)); // [5, 'Chelsey Dietrich', 'Kamren', 'Lucio_Hettinger@annie.ca']
console.log(Object.entries(user));
/*
[
['id', 5]
['name', 'Chelsey Dietrich']
['username', 'Kamren']
['email', 'Lucio_Hettinger@annie.ca']
]
*/
// キーと値のペアを反復処理の中で扱う
Object.keys(user).map((k) => {
console.log(k, user[k]);
});
Object.entries(user).map(([k, v]) => {
console.log(k, v);
});