迹忆客 专注技术分享

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

如何在 JavaScript 中比较两个数组

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

本教程介绍了三种在 JavaScript 中比较数组的方法。有多种方法,但性能可能有所不同。

JavaScript 为我们提供了在现有类中添加新属性和方法的功能。我们可以使用 Array.prototype 将我们的自定义方法 equals 添加到 Array 对象中。

在下面的例子中,我们将首先检查两个数组的长度,然后比较每个项目。我们也在检查 a1a2 是否是 Array 实例,因为如果不是,它们就不一样。最后,我们使用 a1.equals(a2) 的方法来比较第一个数组和第二个数组。

例子:a1.equals(a2)

var a1 = [1, 2, 3];
var a2 = [1, 2, 3];
var a3 = [1, 2, 3, 4];

Array.prototype.equals = function (getArray) {
  if (this.length != getArray.length) return false;

  for (var i = 0; i < getArray.length; i++) {
    if (this[i] instanceof Array && getArray[i] instanceof Array) {
      if (!this[i].equals(getArray[i])) return false;
    } else if (this[i] != getArray[i]) {
      return false;
    }
  }
  return true;
};

console.log("Comparing a1 and a2", a1.equals(a2));
console.log("Comparing a1 and a3", a1.equals(a3));

输出:

Comparing a1 and a2 true
Comparing a1 and a3 false

另一种比较两个数组的技术是先将它们投向字符串类型,然后再进行比较。JSON 是用来从/到 web 服务器传输数据的,但我们可以在这里使用它的方法。我们可以使用 JSON.stringify()Array 转换为 string。由于现在 a1a2 都是字符串,我们可以使用 === 查看它们是否相等。

例子:

var a1 = [1, 2, 3];
var a2 = [1, 2, 3];
var a3 = [1, 2, 3, 4];

console.log("Comparing a1 and a2", JSON.stringify(a1) === JSON.stringify(a2));
console.log("Comparing a1 and a3", JSON.stringify(a1) === JSON.stringify(a3));

输出:

Comparing a1 and a2 true
Comparing a1 and a3 false

循环是 JavaScript 中比较数组的最传统的方式,因为它涉及到在数组中循环,然后将每一个元素相互比较,检查它们是否匹配。

为了使其更简洁,我们可以使用函数,然后返回 boolean 作为结果。

举个例子,我们可以使用函数,然后返回 boolean 结果。

var a1 = [1, 2, 3];
var a2 = [1, 2, 3]; 
var a3 = [1, 2, 3, 4];

const getResult = function (a1, a2) {
   var i = a1.length;
   if (i != a2.length) return false;

   while (i--) {
     if (a1[i] !== a2[i]) return false;
   }
   return true;
 };

console.log("Comparing a1 and a2", getResult(a1, a2));
console.log("Comparing a1 and a3", getResult(a1, a3));

输出:

Comparing a1 and a2 true
Comparing a1 and a3 false

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便