I have this code here:
const myMap = new Map()
myMap.set("banana", true)
myMap.set("apple", true)
Object.keys(myMap).forEach(key => {myMap.set(key, false)})
console.log(myMap.get("banana")) // returns true should return false
console.log(myMap.get("apple")) // returns true should return false
I want to set all the values of this Map
to false
, but it doesn't work at all.
I've tried something like this:
Object.keys(myMap).forEach(key => myMap[key] = false)
but this doesn't work either.
Is there a way to fill all keys of a Map
to a specific value?
You have to specifically call
Map.set
in order to set a value in a Map.myMap[key] = value
will only work for plain objects. You should also usemyMap.keys()
to get an iterator of the map's keys: