latest ECMAScript proposals and releases (as of ECMAScript 2024), several enhancements have been made to built-in objects like Set

JavaScript continues to evolve, and in the latest ECMAScript proposals and releases (as of ECMAScript 2024), several enhancements have been made to built-in objects like Set. Here are some new or recent features involving Set and other collections:

1. New Set Methods (Stage 4 — Finalized for ECMAScript 2024)

The following methods have been added to the Set prototype, making Set operations easier and more declarative:

a. Set.prototype.union(otherSet)

Returns a new set containing all elements from the original and the other set.

javascript

const a = new Set([1, 2]); const b = new Set([2, 3]); const result = a.union(b); // Set {1, 2, 3}

b. Set.prototype.intersection(otherSet)

Returns a new set with only the elements common to both sets.

javascript

const result = a.intersection(b); // Set {2}

c. Set.prototype.difference(otherSet)

Returns a new set with elements from the original set that are not in the other set.

javascript

const result = a.difference(b); // Set {1}

d. Set.prototype.symmetricDifference(otherSet)

Returns a new set with elements that are in either of the sets, but not both.

javascript

const result = a.symmetricDifference(b); // Set {1, 3}

e. Set.prototype.isSubsetOf(otherSet)

Checks if all elements in the current set exist in the other set.

javascript

a.isSubsetOf(b); // false

f. Set.prototype.isSupersetOf(otherSet)

Checks if all elements in the other set exist in the current set.

javascript

a.isSupersetOf(b); // false

g. Set.prototype.isDisjointFrom(otherSet)

Checks if the two sets have no elements in common.

javascript

a.isDisjointFrom(b); // false

2. Map.prototype Additions (Bonus)

Although you asked about Set, it’s worth noting that similar convenience methods are proposed or being considered for Map (e.g., .mapValues(), .filter(), etc.).


new and upcoming methods for Map.prototype, based on current ECMAScript proposals (some already at Stage 3+ and widely supported via polyfills or in newer engines):


New Map Methods (Proposed — Stage 3)

These methods make working with Map objects more like working with arrays:


1. Map.prototype.map(callbackFn)

Returns a new Map with the same keys, but transformed values based on the callback.

javascript

const map1 = new Map([['a', 1], ['b', 2]]); const result = map1.map(([key, value]) => [key, value * 10]); // result: Map { 'a' => 10, 'b' => 20 }

2. Map.prototype.filter(callbackFn)

Returns a new Map with only the entries that pass the callback condition.

javascript

const result = map1.filter(([key, value]) => value > 1); // result: Map { 'b' => 2 }

3. Map.prototype.reduce(callbackFn, initialValue)

Reduces the map entries like Array.prototype.reduce.

javascript

const total = map1.reduce((acc, [key, value]) => acc + value, 0); // total: 3

4. Map.prototype.some(callbackFn)

Returns true if any entry passes the test.

javascript

map1.some(([key, value]) => value === 2); // true

5. Map.prototype.every(callbackFn)

Returns true if all entries pass the test.

javascript

map1.every(([key, value]) => typeof value === 'number'); // true

6. Map.prototype.find(callbackFn)

Returns the first entry (as [key, value]) that satisfies the callback.

javascript

map1.find(([key, value]) => value > 1); // ['b', 2]

Compatibility Note:

  • These methods are Stage 3 (as of 2024) and are not yet standard in all browsers.

  • You can polyfill them using libraries like core-js or use them in environments like Babel or TypeScript with appropriate settings.

No comments:

Post a Comment

Best for you