我需要在哪里,但没有案例.例如,我想找一些没有名字“莎士比亚”的剧本:
_.where(listOfPlays,{author: !"Shakespeare",year: 1611}); ^^^^^^^^^^^^^ NOT Shakespeare
我怎么能用下划线做到这一点?
解决方法
_.filter(listOfPlays,function(play) { return play.author !== 'Shakespeare' && play.year === 1611; });
http://underscorejs.org/#filter
哪里只是一个方便的过滤器包装器:
// Convenience version of a common use case of `filter`: selecting only objects // containing specific `key:value` pairs. _.where = function(obj,attrs) { return _.filter(obj,_.matches(attrs)); };