0
0

More than 3 years have passed since last update.

JavaScript(ES2017 以降)でオブジェクトに Array のメソッドを適用する

Posted at

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 でもお好きなメソッドを使える。

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