ES2017 で導入された Object.entries
は、オブジェクトを受け取って [key, value]
の配列を返す。これを使うと、例えばオブジェクトに対するループ処理が簡単に書ける。
const obj = {
foo: 1,
bar: 2
};
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} = ${value}`);
});
// foo = 1
// bar = 2
forEach
だけでなく、sort
でも map
でも filter
でもお好きなメソッドを使える。