javascript function inArray

前端 未结 5 1665
南方客
南方客 2020-11-28 13:52

I need a javascript function that can take in a string and an array, and return true if that string is in the array..

 function inArray(str, arr){
   ...
 }
         


        
5条回答
  •  眼角桃花
    2020-11-28 14:15

    Something like this?

    function in_array(needle, haystack)
    {
        for(var key in haystack)
        {
            if(needle === haystack[key])
            {
                return true;
            }
        }
    
        return false;
    }
    

提交回复
热议问题