首页 > 语言 > JavaScript > 正文

JavaScript:ES2019 的新特性(译)

2024-05-06 15:36:04
字体:
来源:转载
供稿:网友

作为最流行的编程语言和最重要的 Web 开发语言之一,JavaScript 不断演变,每次迭代都会得到一些新的内部更新。让我们来看看 ES2019 有哪些新的特性,并加入到我们日常开发中:

Array.prototype.flat()

Array.prototype.flat() 递归地将嵌套数组拼合到指定深度。默认值为 1,如果要全深度则使用 Infinity 。此方法不会修改原始数组,但会创建一个新数组:

const arr1 = [1, 2, [3, 4]];arr1.flat(); // [1, 2, 3, 4]const arr2 = [1, 2, [3, 4, [5, 6]]];arr2.flat(2); // [1, 2, 3, 4, 5, 6]const arr3 = [1, 2, [3, 4, [5, 6, [7, 8]]]];arr3.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8]

flat() 方法会移除数组中的空项:

const arr4 = [1, 2, , 4, 5];arr4.flat(); // [1, 2, 4, 5]

Array.prototype.flatMap()

flatMap() 方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 Array.prototype.map 和 深度值为 1的 Array.prototype.flat 几乎相同,但 flatMap 通常在合并成一种方法的效率稍微高一些。

const arr1 = [1, 2, 3];arr1.map(x => [x * 4]); // [[4], [8], [12]]arr1.flatMap(x => [x * 4]); // [4, 8, 12]

更好的示例:

const sentence = ["This is a", "regular", "sentence"];sentence.map(x => x.split(" ")); // [["This","is","a"],["regular"],["sentence"]]sentence.flatMap(x => x.split(" ")); // ["This","is","a","regular", "sentence"]// 可以使用 归纳(reduce) 与 合并(concat)实现相同的功能sentence.reduce((acc, x) => acc.concat(x.split(" ")), []);

String.prototype.trimStart() 和 String.prototype.trimEnd()

除了能从字符串两端删除空白字符的 String.prototype.trim() 之外,现在还有单独的方法,只能从每一端删除空格:

const test = " hello ";test.trim(); // "hello";test.trimStart(); // "hello ";test.trimEnd(); // " hello";
trimStart() :别名 trimLeft(),移除原字符串左端的连续空白符并返回,并不会直接修改原字符串本身。 trimEnd() :别名 trimRight(),移除原字符串右端的连续空白符并返回,并不会直接修改原字符串本身。

Object.fromEntries

将键值对列表转换为 Object 的新方法。

它与已有 Object.entries() 正好相反,Object.entries()方法在将对象转换为数组时使用,它返回一个给定对象自身可枚举属性的键值对数组。

但现在您可以通过 Object.fromEntries 将操作的数组返回到对象中。

下面是一个示例(将所有对象属性的值平方):

const obj = { prop1: 2, prop2: 10, prop3: 15 };// 转化为键值对数组:let array = Object.entries(obj); // [["prop1", 2], ["prop2", 10], ["prop3", 15]]

将所有对象属性的值平方:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选