JS在数组中找到多个值

时间:2019-03-14 11:08:33

标签: javascript jquery arrays

我有一个数组,值是:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

然后我要检查此到数组中:

14,15,16,17

我尝试了这个但没有用:

function contains(a, toFind) {
    for (var i = 0; i < a.length; i++) {
        if (equalArray(a[i], toFind)) {
            return true;
        }
    }
    return false;
}

function equalArray(a, b) {
    if (a.length === b.length) {
        for (var i = 0; i < a.length; i++) {
            if (a[i] !== b[i]) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

使用包含。

let a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];

console.log(a.includes(14));
console.log(a.includes(15));
console.log(a.includes(16));
console.log(a.includes(17));