我的Javascript映射对象将每个键映射到一个数组。我在更新数组时遇到问题。
我尝试仅使用javascript对象。
const temp = {"map":[1], "what":[1,2]};
temp["what"].push(3);
console.log(temp);
它可以工作,但是我仍然想知道是否有一种更新地图对象的方法。
我的原始代码是这样的:
const temp = new Map();
temp.set("what", [1]);
temp["what"].push(2);
console.log(temp);
期望:{“ what” => [1,2]}
实际结果:
VM6258:1 Uncaught TypeError: Cannot read property 'push' of undefined
at <anonymous>:1:12
答案 0 :(得分:4)
您需要将get
与Map
一起使用,即
temp.get("what").push(2)