How to fill a Javascript object literal with many static key/value pairs efficiently?

前端 未结 5 1788
-上瘾入骨i
-上瘾入骨i 2021-01-31 15:36

The typical way of creating a Javascript object is the following:

var map = new Object();
map[myKey1] = myObj1;
map[myKey2] = myObj2;

I need to

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 16:12

    In ES2015 a.k.a ES6 version of JavaScript, a new datatype called Map is introduced.

    let map = new Map([["key1", "value1"], ["key2", "value2"]]);
    map.get("key1"); // => value1
    

    check this reference for more info.

提交回复
热议问题