1.Object.fromEntries()
Object.fromEntries()
方法可以将 二维数组 和 Map 转换成对象
(1) 二维数组
1 2 3 4 5 6 7 8
| const result = Object.fromEntries([ ["name", "Tom"], ["age", 20], ["address", "street"] ]); console.log(result);
|
(2) Map
1 2 3 4 5 6 7 8
| const m = new Map(); m.set("name", "Tom"); m.set("age", 20); m.set("address", "street"); const result = Object.fromEntries(m); console.log(result);
|
(3) ES8 Object.entries()
Object.fromEntries()
与 Object.entries()
互为逆运算
1 2 3 4 5 6 7 8 9 10 11 12 13
| const arr = Object.entries({ name: "Tom", age: 20, address: "street" }); console.log(arr);
|
2.trimStart() 与 trimEnd()
1 2
| trimStart(): 去除字符串左侧空格 trimEnd(): 去除字符串右侧空格
|
1 2 3 4 5 6 7 8 9
| let str = " Hello world 123 ";
console.log(str.trim());
console.log(str.trimStart());
console.log(str.trimEnd());
|
3.flat() 与 flatMap()
(1) flat()
1 2 3 4 5 6 7 8
|
const arr1 = [1, 2, [3, 4], 5, [6, 7, 8], 9]; console.log(arr1.flat());
const arr2 = [1, 2, [3, 4, [5, 6, 7], 8], 9]; console.log(arr2.flat()); console.log(arr2.flat(2));
|
(2) flatMap()
1 2 3
| const arr3 = [1, 2, 3, 4]; const result = arr3.map(item => item * 10); console.log(result);
|
1 2 3 4 5 6 7 8 9 10 11 12
| const arr3 = [1, 2, 3, 4]; const result = arr3.map(function(item) { return [item * 10]; }); console.log(result);
|
使用 flatMap() 对元素进行展开
1 2 3 4
| const arr4 = [1, 2, 3, 4]; const result = arr4.flatMap(item => [item * 10]); console.log(result);
|
4.Symbol.prototype.description
Symbol 对象的 description 属性值为 Symbol 对象的字符串的值
1 2 3 4 5
| let s = Symbol("SymbolTestObj");
console.log(s.description);
|