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
1 Answer
Reset to default 7You need to use get
with Map
, i.e.
temp.get("what").push(2)