Map upsert

Specification
Proposal repo

Modules

es.map.get-or-insert, es.map.get-or-insert-computed, es.weak-map.get-or-insert and es.weak-map.get-or-insert-computed

Built-ins signatures

class Map {
  getOrInsert(key: object | symbol, value: any): any;
  getOrInsertComputed(key: object | symbol, (key: any) => value: any): any;
}

class WeakMap {
  getOrInsert(key: object | symbol, value: any): any;
  getOrInsertComputed(key: object | symbol, (key: any) => value: any): any;
}

Entry points

core-js/proposals/map-upsert-v4
core-js(-pure)/es|stable|actual|full/map/get-or-insert
core-js(-pure)/es|stable|actual|full/map/get-or-insert-computed
core-js(-pure)/es|stable|actual|full/weak-map/get-or-insert
core-js(-pure)/es|stable|actual|full/weak-map/get-or-insert-computed

TypeScript type definitions

@core-js/types/proposals/map-upsert

Examples

const map = new Map([['a', 1]]);

map.getOrInsert('a', 2); // => 1

map.getOrInsert('b', 3); // => 3

map.getOrInsertComputed('a', key => key); // => 1

map.getOrInsertComputed('c', key => key); // => 'c'

console.log(map); // => Map { 'a': 1, 'b': 3, 'c': 'c' }