How can I use Underscore.js to filter an array based on a specific key and value? Currently, my code searches for all fields' values, but I only want to search based on the key and value. Can you provide guidance on how to achieve this using Underscore.js?
I would appreciate any help or suggestions. Thank you.
var sortedArray = _.sortBy(_.filter(arr, function (obj) {
return _.values(obj).some(function (el) {
return (typeof el ==="string" && el.match(new RegExp(searchStr, "i")));
});
}), function (obj){
return obj[colName];
});
}
{
"recordsTotal": 5,
"recordsFiltered": 5,
"aaData": [
{
"firstname": "Pradeep",
"lastname": "Kumar",
"city": "Bangalore",
"country": "India"
},
{
"firstname": "John",
"lastname": "Wells",
"city": "Calcutta",
"country": "India"
},
{
"firstname": "Praveen",
"lastname": "Garg",
"city": "columbo",
"country": "Srilanka"
},
{
"firstname": "Joe",
"lastname": "Wells",
"city": "Luton",
"country": "UK"
},
{
"firstname": "Rita",
"lastname": "Wahlin",
"city": "houston",
"country": "USA"
}
]
}