admin管理员组

文章数量:1327524

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)

本文标签: How to modify the value of a JavaScript mapStack Overflow