I get an array of objects that looks like this:
var collection = [
{
name: 'hello',
color: 'blue'
},
{
name: 'world',
color: 'brown'
}, .... {thousands more}
];
What would be the proper way to use underscore to find out if any of the objects in the array has a value for the 'name' key equal to some regular expression?
_.contains(collection, '/goodbye/i'); <-- this won't work ->
How to tell it to use the 'name' key for searching?
You could do:
filter = function (collection, key, regex) {
return _.filter(collection, function(obj){ return obj[key].match(regex);});
};
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.