Javascript- Matching values from array to substring of variable

后端 未结 3 1166
日久生厌
日久生厌 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);
                }
            }   
    
    0 讨论(0)
  • 2021-01-26 01:54

    you don't need an if statement for it

    return array.indexOf(something)!=-1
    
    0 讨论(0)
  • 2021-01-26 01:55

    I would do this like as follows;

    var array = ['Potato', 'Cheese', 'Bikini', 'Truck', 'Express'],
    something = 'Potato Thief',
         test = array.some(e => something.includes(e));
    console.log(test)

    0 讨论(0)
提交回复
热议问题