How to search an array in Jquery like SQL LIKE %value% statement

前端 未结 8 2327
一个人的身影
一个人的身影 2020-12-08 00:57

I have an array with some values. How can I search that array using jquery for a value which is matched or close to it?

var a = [\"foo\",\"fool\",\"cool\",\"         


        
相关标签:
8条回答
  • 2020-12-08 02:01

    You can use find method on array if you want to match any regex :

    Array.find(x => /^left:/.test(x)
    
    0 讨论(0)
  • function find(arr) {
        var result = [];
    
        for (var i in arr) {
            if (arr[i].match(/oo/)) {
                result.push(arr[i]);
            }
        }
    
        return result;
    }
    
    window.onload = function() {
        console.log(find(['foo', 'fool', 'cool', 'god']));
    };
    

    It prints ["foo", "fool", "cool"]

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