当值应该是Javascript中的列表时,如何在映射中设置/添加键值对

时间:2019-06-03 22:07:41

标签: javascript merge immutable.js

我正在通过以下键值对进行映射:

a => a1
a => a2 
a => a3

b => b1
b => b2 
b => b3

c => c1
c => c2 
c => c3

现在我有一张地图:

const myobject = new Map()

我需要在我的地图中设置以上数据,以使每个键都具有一个类似于以下列表的值:

a => list[a1, a2, a3] 
b => list[b1, b2, b3] 
c => list[c1, c2, c3] 

这是实际代码:

const serviceResponses = new Map();
await Promise.all(allPromise.map(promises => {
   promises.then(promise => promise.map(([stateName, item]) => {
            // below is the part I am trying to set stateName (string)  and 
            // result (it should be an array) into serviceResponses which is a 
            // map. note: result always has the same number of items. in this 
            // example the number of items is 3, but it can be any number. 
            item[0].then(result => {
                        serviceResponses.set(stateName, result);
                });
                return serviceResponses;
           }));

我浏览了Immutablr.js api文档,并且已经尝试了setIn,set,MergeIn和MergeDeep。 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

From the MDN

如果您只是想知道如何设置值,请执行以下操作:

myobject.set('a', [a1, a2, a3]);