1。
const numbers = [1, 2, 3, 4, 5]; const lastEven = numbers.findLast(num => num % 2 === 0); // 4 // Find last user who is 18 years old in large array const users = [/* array with 10000 users */]; users.findLast(user => user.age === 18);
const lastEvenIndex = numbers.findLastIndex(num => num % 2 === 0); // 3
如果您在JavaScript中编写命令行工具,您将感谢HashBangs的新支持。通过添加#!在文件的顶部,您可以直接指定解释器,使您的脚本可执行而无需外部命令。
符号为feepMap键
#!/usr/bin/env node console.log("Hello, world!");
这种增强使虚弱映射更加通用,尤其是当您需要符号提供的独特的无碰撞键时。
通过新组方法进行分组数组元素变得容易得多。
const numbers = [1, 2, 3, 4, 5]; const lastEven = numbers.findLast(num => num % 2 === 0); // 4 // Find last user who is 18 years old in large array const users = [/* array with 10000 users */]; users.findLast(user => user.age === 18);
const Animal = [ {type:'哺乳动物',名称:'dog'}, {type:'bird',名称:'sparrow'}, {type:'哺乳动物',名称:'cat'}, ]; const grouped = Animal.group(({type})=> type); console.log(分组); // { //哺乳动物:[{type:'哺乳动物',name:'dog'},{type:'mammal',name:'cat'}], //伯德:[{type:'bird',名称:'sparrow'}]] //}
排序阵列刚好有很多清洁。与变化原始数组的类别不同,TOSORTED返回一个新的排序阵列和toreversed返回一个新的反向数组,而原始的未触及。
const animals = [ { type: 'mammal', name: 'dog' }, { type: 'bird', name: 'sparrow' }, { type: 'mammal', name: 'cat' }, ]; const grouped = animals.group(({ type }) => type); console.log(grouped); // { // mammal: [{ type: 'mammal', name: 'dog' }, { type: 'mammal', name: 'cat' }], // bird: [{ type: 'bird', name: 'sparrow' }] // }array.prototype.with
7。
const arr = [3, 1, 4, 1, 5]; const sortedArr = arr.toSorted(); console.log(sortedArr); // [1, 1, 3, 4, 5] console.log(arr); // [3, 1, 4, 1, 5] let data = [/* important data that shouldn't be modified */]; let reversedData = data.toReversed(); // Safely reverse
这是处理异步操作的一种整洁而简洁的方法,尤其是当您需要从其构造函数外部控制承诺的结果时。
ES2023的浏览器支持
Safari尚未支持这些功能。
const numbers = [1, 2, 3, 4, 5]; const lastEven = numbers.findLast(num => num % 2 === 0); // 4 // Find last user who is 18 years old in large array const users = [/* array with 10000 users */]; users.findLast(user => user.age === 18);node.js
const arr = [3, 1, 4, 1, 5]; const sortedArr = arr.toSorted(); console.log(sortedArr); // [1, 1, 3, 4, 5] console.log(arr); // [3, 1, 4, 1, 5] let data = [/* important data that shouldn't be modified */]; let reversedData = data.toReversed(); // Safely reverse
transpilers:
结论
ES2023带来了一系列新功能,使JavaScript更强大,更易于使用。从增强的阵列方法到更好的承诺处理,这些更新都在于使您的代码清洁和更有效。随着JavaScript的不断发展和发展,保持最新的这些更改,确保您始终充分利用语言。mdn
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3