迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

Javascript 中数组 Array 的新方法

作者:迹忆客 最近更新:2022/10/24 浏览次数:

在之前的文章中,我们介绍了 Javascript 将对象和数组存储在堆存储中。 这意味着一个数组被创建一次,以后对它的任何更新都会更新那个原始数组。 例如:

let myArray = [ 1, 2, 3 ];
let newArray = myArray;

newArray[0] = 2;

// 二者都返回 [ 2, 2, 3 ]
console.log(newArray, myArray);

因此,我们经常发现自己制作副本或克隆数组从而在不影响原始数据的情况下对其进行更改。 一种常见的方法是使用三点...运算符:

let myArray = [ 1, 2, 3];
let newArray = [...myArray];

newArray[0] = 2;

// 返回 [ 2, 2, 3 ] 和 [ 1, 2, 3 ]
console.log(newArray, myArray);

由于克隆如此普遍,已经编写了一个新的规范,该规范已达到第 3 阶段,这将为我们提供许多复制然后更改数组的新方法。 该提案称为“通过复制更改数组”提案。 让我们来看看它是如何工作的。

支持

目前,任何主流浏览器或 Node.JS 版本都不支持这些功能。 不过,我们可以期待它们很快会得到支持,因为该规范已进入 Javascript 提案流程的第 3 阶段。 此部分将随着支持的变化而更新。

话虽如此,确实存在一个 polyfill 来重新创建此提案的行为,我们现在可以使用它。


通过复制(Copy)方法新建更改数组

新提案增加了 4 个新方法,它们将复制一个数组,然后以某种方式对其进行更改。 这些方法如下:

  • Array.prototype.toReversed() - 克隆一个数组,然后反转它
  • Array.prototype.toSorted(compareFn) - 克隆一个数组,然后对其进行排序。
  • Array.prototype.toSpliced(start, deleteCount, ...items) - 克隆一个数组,并以某种方式拼接它。
  • Array.prototype.with(index, value) - 克隆一个数组,并在其中的某处添加一个新元素。

所有这些方法也适用于 TypedArray 数据,例如 Int8Array

toReversed()

toReversed 完全按照它所说的去做——它需要一个数组,然后反转它。 由于这个函数克隆了数组,我们不需要做任何复制来创建原始数组的新版本:

let x = [ 1, 2, 3 ];
let y = x.toReversed();

// 返回 [ 1, 2, 3 ], [ 3, 2, 1 ]
console.log(x, y);

toSorted()

toSorted 是 sort() 的非破坏性版本,这意味着它不会改变原始数组。 默认情况下,将采用一个数组并对其进行数字排序。 例如:

let x = [ 5, 3, 4, 2, 1 ];
let y = x.toSorted(); // [ 1, 2, 3, 4, 5 ]

它还接受一个比较函数,其工作方式与 Javascript 中的 sort() 方法相同,并且在处理对象数组时很有用。 例如:

let x = [
    { value: 0 },
    { value: 4 },
    { value: 2 },
    { value: 3 }
];

// y 成为:
// [
//    { value: 0 },
//    { value: 2 },
//    { value: 3 },
//    { value: 4 }
// ]
let y = x.toSorted((a, b) => {
    return a.value - b.value
});

toSpliced()

toSpliced() 是 splice() 的非破坏性版本,这意味着它不会改变原始数组。 它接受三个参数:

  • start - 开始的位置。
  • deleteCount - 要删除的元素数。
  • ...items - 删除后要在起始位置插入的任何项目。

示例如下

let x = [ "Dog", "Cat", "Zebra", "Bat", "Tiger", "Lion" ];

// y 为 [ "Dog", "Snake", "Bat", "Tiger", "Lion" ]
let y = x.toSpliced(1, 2, "Snake");

// z 为 [ "Dog, "Tiger", "Lion" ]
let z = x.toSpliced(1, 3);

以上是这些复制函数如何有用的完美示例 - 使用原始 splice() 方法,我们将从 x 中删除元素并永久更改它们。 使用 toSpliced(),我们可以多次改变 x,而不用担心它的内容改变。

with()

最后,我们有 with(),它只是更改现有数组中的一个元素。 本质上,拿 Array A 并用其他东西展示它。 这是它的一个例子。

let x = [ "Dog", "Cat", "Lizard" ]
// y 现在为 [ "Dog", "Zebra", "Lizard" ]
let y = x.with(1, "Zebra")
// z 现在为 [ "Tiger", "Cat", "Lizard" ]
let z = x.with(0, "Tiger")

同样,我们已经能够从一个初始数组创建多个新数组,而不必每次都创建一个副本。

总结

这些数组添加是毫无争议的,对于通常只维护数组或对象的一个副本的语言来说,这是非常有必要的添加。 使用这些新方法,Javascript 消除了代码中的混乱和复杂性。 尽管没有像 Temporal 这样的变化那么大,但它仍然是该语言的一个受欢迎的补充。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便