js es10 新特性

原文链接: js es10 新特性

上一篇: python acm 坐标移动 正则表达式

下一篇: python opencv 交叉淡化

原文
ES10 功能完全指南

在过去的整数值大于9007199254740992不支持。如果超出,则该值将锁定为 MAX_SAFE_INTEGER + 1

加法需要类型相同

const limit = Number.MAX_SAFE_INTEGER;
console.log(limit) // 9007199254740991
console.log(limit + 1) // 9007199254740992
console.log(limit + 2) // 9007199254740992

const larger = 9007199254740991n;
console.log(larger, larger + 2n) // 9007199254740991n 9007199254740993n

const integer = BigInt(9007199254740991); // initialize with number
console.log(integer, integer + 2n) // 9007199254740991n 9007199254740993n

const same = BigInt("9007199254740991"); // initialize with "string" 9007199254740991n
console.log(same, same + 2n) // 9007199254740991n 9007199254740993n

console.log(typeof 1, typeof 1n) // number bigint

数组扁平化

let multi = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]];
multi.flat();               // [1,2,3,4,5,6,Array(4)]
multi.flat().flat();        // [1,2,3,4,5,6,7,8,9,Array(3)]
multi.flat().flat().flat(); // [1,2,3,4,5,6,7,8,9,10,11,12]
multi.flat(Infinity);       // [1,2,3,4,5,6,7,8,9,10,11,12]

let array = [1, 2, 3, 4, 5]
array.map(x => [x, x * 2])
// [Array(2), Array(2), Array(2)]
// 0: (2)[1, 2]
// 1: (2)[2, 4]
// 2: (2)[3, 6]
// 3: (2)[4, 8]
// 4: (2)[5, 10]

array.flatMap(v => [v, v * 2])
// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]

对象键值对遍历

let obj = {apple: 10, orange: 20, banana: 30};
let entries = Object.entries(obj);
// entries;
// (3) [Array(2), Array(2), Array(2)]
// 0: (2) ["apple", 10]
// 1: (2) ["orange", 20]
// 2: (2) ["banana", 30]
let fromEntries = Object.fromEntries(entries);
// { apple: 10, orange: 20, banana: 30 }

稳定排序

var fruit = [
  {name: "Apple", count: 13,},
  {name: "Pear", count: 12,},
  {name: "Banana", count: 12,},
  {name: "Strawberry", count: 11,},
  {name: "Cherry", count: 11,},
  {name: "Blackberry", count: 10,},
  {name: "Pineapple", count: 10,}
];
// Create our own sort criteria function:
let my_sort = (a, b) => a.count - b.count;
// Perform stable ES10 sort:
let sorted = fruit.sort(my_sort);
console.log(sorted);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值