檢查數組中是否存在元素[重複] - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章