JS: Map.prototype.getOrInsert
(new in ECMAScript 2026)
mapObj.get(key, newVal)-
get the value of the key, if not exist, insert it and return the value.
const xx = new Map([[4, "n4"], [5, "n5"]]); // key exist console.assert(xx.getOrInsert(5, "n5") === "n5"); // key no exist console.assert(xx.getOrInsert(6, "n6") === "n6"); console.assert(xx.getOrInsert(6) === "n6");
JavaScript. Methods for map object
- JS: Map.groupBy
- JS: Map.prototype.size
- JS: Map.prototype.get
- JS: Map.prototype.getOrInsert
- JS: Map.prototype.getOrInsertComputed
- JS: Map.prototype.set
- JS: Map.prototype.has
- JS: Map.prototype.delete
- JS: Map.prototype.clear
- JS: Map.prototype.forEach
- JS: Map.prototype.keys
- JS: Map.prototype.values
- JS: Map.prototype.entries