最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to modify the value of a JavaScript map? - Stack Overflow

programmeradmin2浏览0评论

My Javascript map object maps each key to an array. I have an issue with updating the array.

I tried to use a javascript object only.

const temp = {"map":[1], "what":[1,2]};
temp["what"].push(3);
console.log(temp);

It works but I still want to know whether there is a way to update the map object.

My original code is like this:

const temp = new Map();
temp.set("what", [1]);
temp["what"].push(2);
console.log(temp);

expect: {"what"=>[1,2]}

actual result:

VM6258:1 Uncaught TypeError: Cannot read property 'push' of undefined at <anonymous>:1:12

My Javascript map object maps each key to an array. I have an issue with updating the array.

I tried to use a javascript object only.

const temp = {"map":[1], "what":[1,2]};
temp["what"].push(3);
console.log(temp);

It works but I still want to know whether there is a way to update the map object.

My original code is like this:

const temp = new Map();
temp.set("what", [1]);
temp["what"].push(2);
console.log(temp);

expect: {"what"=>[1,2]}

actual result:

VM6258:1 Uncaught TypeError: Cannot read property 'push' of undefined at <anonymous>:1:12

Share Improve this question asked Dec 28, 2018 at 0:35 Lusha LiLusha Li 1,1682 gold badges13 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You need to use get with Map, i.e.

temp.get("what").push(2)
发布评论

评论列表(0)

  1. 暂无评论