原文链接: js es10 新特性
下一篇: 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);