SC2*_*C28 2 javascript underscore.js
我得到一个看起来像这样的对象数组:
var collection = [
{
name: 'hello',
color: 'blue'
},
{
name: 'world',
color: 'brown'
}, .... {thousands more}
];
Run Code Online (Sandbox Code Playgroud)
使用下划线找出数组中的任何对象是否具有"name"键的值等于某个正则表达式的正确方法是什么?
_.contains(collection, '/goodbye/i'); <-- this won't work ->
如何告诉它使用'name'键进行搜索?
phe*_*nal 11
你可以这样做:
filter = function (collection, key, regex) {
return _.filter(collection, function(obj){ return obj[key].match(regex);});
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8717 次 |
| 最近记录: |