js判断数组是否包含某元素

let arr1 = [1, 2, 3];
arr1.includes(2)
true

arr1.indexOf(2)>0
true

util.oneOf = function (ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};

util.showThisRoute = function (itAccess, currentAccess) {
    if (typeof itAccess === 'object' && Array.isArray(itAccess)) {
        return util.oneOf(currentAccess, itAccess);
    } else {
        return itAccess === currentAccess;
    }
};
posted @ 2020-12-10 19:39  船长博客  阅读(900)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!