JavaScript – Update Value for Specific Key in Map
To update value for a specific key in Map in JavaScript, call set() method on this Map and pass the specific key and new value as arguments.
Syntax
The syntax to update the value for a specific key in a Map map
using set() method is
</>
Copy
map.set(key, new_value)
Examples
In the following example, we update the value for key 'a'
with a new value of 88
.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var map1 = new Map([
['a', 10],
['b', 20]
]);
map1.set('a', 88);
var displayOutput = '';
map1.forEach (function(value, key) {
displayOutput += key + ' - ' + value + '\n';
});
document.getElementById('output').innerHTML += displayOutput;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to update the value for a specific key in a Map in JavaScript using set() method, with examples.