Skip to content

Add getOrInsert and getOrInsertCallable to map & weak_map #4435

@jasonmilad

Description

@jasonmilad

ECMASCript feature
Upsert proposal for maps has made it to stage three. This ticket is tracking that implementation.

Example code
Give a code example that should work after the implementation of this feature.

This code should now work:

// Currently
let counts = new Map();
if (counts.has(key)) {
  counts.set(key, counts.get(key) + 1);
} else {
  counts.set(key, 1);
}

// Using getOrInsert
let counts = new Map();
counts.set(key, counts.getOrInsert(key, 0) + 1);

// Using getOrInsertComputed
let grouped = new Map();
for (let [key, ...values] of data) {
  grouped.getOrInsertComputed(key, () => []).push(...values);
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions