简体   繁体   中英

how to filter array in jquery javascript

I want to filter jquery array like sql server "Like % %"

var array=[
{"job_category":"hello sir","job_location":"hello dear"},
{"job_category":"dear kumar ","job_location":"sir"},
{"job_category":"testts ssss ss","job_location":"hello test"}

]

var keyword="hello"

how to find keyword 'hello' in this array

Assuming you meant to use an array, you could use array.prototype.filter :

var array=[
    {"job_category":"hello sir","job_location":"hello dear"},
    {"job_category":"dear kumar ","job_location":"sir"},
    {"job_category":"testts ssss ss","job_location":"hello test"}
];

var keyword="hello";

var filteredArray = array.filter(function(item) { 
    return item.job_category.indexOf(keyword) != -1 || 
        item.job_location.indexOf(keyword) != -1;
});

Try this code

var arrays=[
    {"job_category":"hello sir","job_location":"hello dear"},
    {"job_category":"dear kumar ","job_location":"sir"},
    {"job_category":"testts ssss ss","job_location":"hello test"}
];

var keyword="hello";
  var search = new RegExp(keywods, "gi");
    var newarrays= $.grep(arrays, function (item) {
        return (item.job_category.match(search) || item.job_location.match(search))
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM