-
-
Notifications
You must be signed in to change notification settings - Fork 456
Open
Labels
Description
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);
}