迹忆客 专注技术分享

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

JavaScript in_array 函数

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

在 JavaScript 中,如果我们希望遍历从数组中找到某个字符串或数字,我们将不得不使用 includes() 方法。在引入这个约定之前,我们通常通过在数组索引上启动一个 for 循环来完成搜索任务。

因为按值传递按引用传递之间存在差异,includes() 方法并没有给我们正确的答案。

我们将学习 includes() 方法的使用,我们将尝试手动创建 includes() 方法功能,并了解如何创建该函数以防我们在本文中对对象进行检查。

includes() 方法通常在开头使用数组名称(我们将在其中搜索元素),然后是 dot(.)。在下一部分中,我们将提到我们的元素。

代码片段:

var name = ['Rayan', 'Emily', 'Sarah'];
var check = 'Rayan';
console.log(name.includes(check));

输出:

使用 includes() 方法从数组中搜索元素

此方法以布尔值返回结果,因此我们的输出将被控制台输出为 truefalse

在此示例中,我们将创建一个数组和一个变量来存储要搜索的元素。我们将创建一个简单的 for 循环来检查我们的元素并以 truefalse 返回结果。

代码片段:

var a = 2;
var b = [1,2,3];
var verdict;
for(var i=0;i<b.length;i++){
  if(b[i]===a){
    verdict = true;
    break;
  }
  else{
    verdict = false;
  }
}
console.log(verdict);

输出:

使用 for 循环检查元素是否存在

JavaScript 有两种数据类型,primitiveobjects。primitive 数据遵循 pass by value,所以我们更容易执行上面的任务,但在 objects 的情况下,传递引用用于比较任何内容。

如果我们遵循一般的检查方式或 includes() 方法,我们将无法得出完美的答案。

代码片段:

function Compare(b, a) {
    if (b.length != a.length) return false;
    var length = a.length;
    for (var i = 0; i < length; i++) {
        if (b[i] !== a[i]) return false;
    }
    return true;
}
function inArray(a, b) {
    var length = b.length;
    for(var i = 0; i < length; i++) {
        if(typeof b[i] == 'object') {
            if(Compare(b[i], a)) return true;
        } else {
            if(b[i] == a) return true;
        }
    }
    return false;
}
var a = [1,2];
var b = [[1,2],3,4];
console.log(inArray(a,b));

输出:

使用函数从对象中检查对象

如果你排除特定的 b[i]a,则值将与 [1,2] 相同。你还会发现数据类型对于两者来说都是对象,但 ab 都持有不同的引用,而且我们知道,对象支持按引用传递

我们不能通过选择传递参考。我们在这里所做的是,在我们的 inArray 函数中,我们已经确定 b 对象中是否有任何对象类型数据。

如果结果为真,我们使用 Compare 函数,我们明确检查 b[i] 对象值和 a 对象值以获得我们更喜欢的输出。

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便