LoginSignup
0
0

More than 3 years have passed since last update.

javascriptのMap.setの戻り値はMap

Posted at

と言うわけなので、

const map = new Map().set(1,"hoge");

と言った感じに値を入れつつ初期化が出来る。

map.set(2,"fuga").set(3,"piyo");

このようにメソッドチェーンで複数追加することも可能。

//配列を特定のパラメータを持ったMapに変換する(重複がない前提)
const groupedMap = someObjectArray.reduce((map,current) => map.set(current.hoge,current),new Map());

reduceと組み合わせてみても面白い書き方が出来る。

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