Fundamentals 14 min read

New ECMAScript 2023 Array Proposals: Non‑Destructive Methods, Grouping, Find‑From‑Last, and fromAsync

The article explains the upcoming ECMAScript 2023 proposals—including non‑destructive array methods (toReversed, toSorted, toSpliced, with), array grouping (group, groupToMap), reverse‑search methods (findLast, findLastIndex), and the asynchronous constructor Array.fromAsync—detailing their stages, usage examples, polyfills, and type signatures for modern JavaScript development.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
New ECMAScript 2023 Array Proposals: Non‑Destructive Methods, Grouping, Find‑From‑Last, and fromAsync

ECMAScript 2023 (the 14th edition) will incorporate several new array features that aim to make array manipulation more functional and less error‑prone. Proposals at stage 3 introduce non‑destructive versions of common mutating methods: Array.prototype.toReversed() , toSorted([compareFn]) , toSpliced(start, deleteCount, ...items) , and with(index, value) . These methods return a new array while leaving the original unchanged, eliminating the need to manually copy arrays before sorting or splicing.

Example usage of toReversed demonstrates that the original array remains intact:

const arr = ['a', 'b', 'c'];
const result = arr.toReversed();
console.log(result); // ['c', 'b', 'a']
console.log(arr);    // ['a', 'b', 'c']

Simple polyfills are provided for each method, adding them to Array.prototype when the native implementation is missing.

Another set of proposals (stage 3) adds array grouping capabilities: Array.prototype.group(callback, thisArg?) returns an object whose keys are group identifiers, while Array.prototype.groupToMap(callback, thisArg?) returns a Map with the same grouping logic but allowing non‑string keys. Example code shows grouping promise settlement results by status and grouping a list of persons by country.

const result = persons.groupToMap(person => person.country);

For reverse searching, the findLast and findLastIndex proposals (stage 3) provide semantics identical to find and findIndex but iterate from the end of the array, avoiding the need to reverse the array first.

const arr = [{v:1},{v:2},{v:3},{v:4},{v:5}];
arr.findLast(e => e.v > 3);      // {v:5}
arr.findLastIndex(e => e.v > 3); // 4

Polyfills for these methods are also included, showing how to implement them manually.

Finally, the Array.fromAsync proposal (stage 3) adds a built‑in way to create an array from an async iterable, returning a promise that resolves to the populated array. The article illustrates its usage with an async generator and compares it to a manual for‑await‑of loop.

const arr = await Array.fromAsync(asyncGen(4)); // [0,2,4,6]

All proposals are linked to their respective GitHub repositories, and the article provides the current stage of each, usage patterns, and reference implementations, making it a comprehensive guide for developers preparing for the next ECMAScript release.

ECMAScriptpolyfillArray MethodsfindLastfromAsyncGroupingJavaScript proposalsnon‑destructive
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.