xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

ES2023 Array new features All In One

ES2023 Array new features All In One

change Array by copy

Array.toReversed()

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const reversed = numbers.toReversed();

console.log("reversed =", reversed);
// reversed = [9, 8, 7, 6, 5, 4, 3, 2, 1]
console.log("original =", numbers);
// original = [1, 2, 3, 4, 5, 6, 7, 8, 9]

image

Array.toSorted()

const numbers = [9, 5, 4, 3, 2, 8, 7, 6, 1];
const sortedArr = numbers.toSorted();

console.log("sorted =", sortedArr);
// sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log("original = ", numbers);
// original = [9, 5, 4, 3, 2, 8, 7, 6, 1];

image

Array.with()

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const replaceWith = numbers.with(1, `✅`);

console.log("with =", replaceWith);
// with = [1, `✅`, 3, 4, 5, 6, 7, 8, 9]
console.log("original =", numbers);
// original = [1, 2, 3, 4, 5, 6, 7, 8, 9]

image

Array.toSpliced()

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const splicedArr = numbers.toSpliced(0, 3);

console.log("toSpliced  =", splicedArr);
// toSpliced  = [4, 5, 6, 7, 8, 9]
console.log("original", numbers);
// original = [1, 2, 3, 4, 5, 6, 7, 8, 9]

image

Array.findLast()

const arr = [{v: 3, i: 1}, {v: 2, i: 2}, {v: 3, i: 3}, {v: 2, i: 4}];
const lastV2 = arr.findLast(obj => obj.v === 2);

console.log(`lastV2 =`, lastV2);
// lastV2 = {v: 2, i: 4}

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast

extend Array

If you extend the built in Array object and use map, flatMap, filter, or concat on an instance, it will return a new instance of the same type.
If you extend an Array and use toSorted, toReversed, toSpliced, or with the result will be a plain Array again.

class customArray extends Array {
  //
}
const languages = new customArray("JavaScript", "TypeScript", "CoffeeScript");
const upcase = languages.map(language => language.toUpperCase());

languages;
// customArray(3) ['JavaScript', 'TypeScript', 'CoffeeScript']
upcase;
// customArray(3) ['JAVASCRIPT', 'TYPESCRIPT', 'COFFEESCRIPT']
console.log(upcase instanceof customArray);
// true

// copy ✅
const reversed = languages.toReversed();
reversed;
// (3) ['CoffeeScript', 'TypeScript', 'JavaScript']
console.log(reversed instanceof customArray);
// false

// recovery ✅
const recovery = customArray.from(reversed);
recovery;
// customArray(3) ['CoffeeScript', 'TypeScript', 'JavaScript']
console.log(recovery instanceof customArray);
// true

image

demos

Finished Proposals

Finished proposals are proposals that have reached stage 4, and are included in the latest draft of the specification.

image

https://github.com/tc39/proposals/blob/main/finished-proposals.md

Ecma TC39

https://github.com/tc39/ecma262

https://github.com/tc39/notes/tree/main/meetings

refs

https://github.com/tc39/proposal-change-array-by-copy

ECMAScript 2023 & Temporal All In One

https://www.cnblogs.com/xgqfrms/p/16927831.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(8)  评论(4编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-09-14 GoPro Hero 11 Black vs DJI Osmo Action 3 All In One
2022-09-14 HTML5 input inputmode All In One
2022-09-14 WebAssembly for Game Development All In One
2022-09-14 free 3D model download website All In One
2021-09-14 多维表格 All In One
2020-09-14 css break-inside
2020-09-14 how to read the system information by using the node cli tool?
点击右上角即可分享
微信分享提示