相关疑难解决方法(0)

如何检查数组是否包含JavaScript中的对象?

找出JavaScript数组是否包含对象的最简洁有效的方法是什么?

这是我知道的唯一方法:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

有没有更好,更简洁的方法来实现这一目标?

这与Stack Overflow问题密切相关问题在JavaScript数组中查找项目的最佳方法是什么?它使用的方法寻找数组中的对象indexOf.

javascript arrays algorithm time-complexity javascript-objects

3778
推荐指数
37
解决办法
241万
查看次数

How do I check a list of words in an if statement for my chrome extension?

I'm working on a chrome extension and I'm using a lot of if statements to check the webpage for a list of words. And I have to continuously type each word over and over again but I'm wondering if there was a way to check the words one time through a list.

Currently I'm kinda working like this:

if (text == "apple")
if (text == "orange")
if (text == "Banana")
if (text == "Cherry")
if (text == "Lime")
Run Code Online (Sandbox Code Playgroud)

and so …

javascript google-chrome-extension

0
推荐指数
1
解决办法
47
查看次数