Skip to main content

Map.prototype.emplace

Less than 1 minutefeaturees-proposal

Map.prototype.emplaceopen in new window

Modules

Types

interface Map<K, V> {
  emplace<T, U>(
    key: any,
    handler: {
      update: (value: V, key: K, target: Map<K, V>) => T;
      insert: (key: K, target: Map<K, V>) => U;
    }
  ): T | U;
}

interface WeakMap<K extends object, V> {
  emplace<T, U>(
    key: any,
    handler: {
      update: (value: V, key: K, target: WeakMap<K, V>) => T;
      insert: (key: K, target: WeakMap<K, V>) => U;
    }
  ): T | U;
}

Entry points

core-js/proposals/map-upsert-stage-2
core-js(-pure)/full/map/emplace
core-js(-pure)/full/weak-map/emplace

Example

Exampleopen in new window:

const map = new Map([["a", 2]]);

map.emplace("a", { update: (it) => it ** 2, insert: () => 3 }); // => 4

map.emplace("b", { update: (it) => it ** 2, insert: () => 3 }); // => 3

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