Javascript- Matching values from array to substring of variable

后端 未结 3 1195
日久生厌
日久生厌 2021-01-26 01:05

I\'m trying to match values from an array to a variable string. So if, for example, I have an array and a variable :

var array = [\'Potato\', \'Cheese\', \'Bikin         


        
3条回答
  •  天涯浪人
    2021-01-26 01:34

    Try this. You have to loop the array and match array elements with the given string

     var array = ['Potato', 'Cheese', 'Bikini', 'Truck', 'Express'];
            var something = 'Potato Thief';
    
    
            for (var i = 0; i < array.length; i++) { 
    
                if(something.search(array[i]) == 0){
                    console.log(true);
                }else{
                    console.log(false);
                }
            }   
    

提交回复
热议问题