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]
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];
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]
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]
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}
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
demos
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
Finished Proposals
Finished proposals are proposals that have reached
stage 4
, and are included in thelatest draft
of the specification.
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, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17703785.html
未经授权禁止转载,违者必究!