检查数组中是否存在元素[重复] - Check if an element is present in an array [duplicate]

问题:

This question already has answers here : 这个问题在这里已经有了答案
Closed 4 years ago . 4年前关闭。

The function I am using now to check this is the following:我现在用来检查的功能如下:

function inArray(needle,haystack)
{
    var count=haystack.length;
    for(var i=0;i<count;i++)
    {
        if(haystack[i]===needle){return true;}
    }
    return false;
}

It works.有用。 What I'm looking for is whether there is a better way of doing this.我正在寻找的是是否有更好的方法来做到这一点。


解决方案:

参考一: https://en.stackoom.com/question/UxPg
参考二: https://stackoom.com/question/UxPg
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章