我目前有以下内容:
flatArray = _.map(items, function(x){ return x.count; });
这将返回一个数组,如下所示:
[6,6,3,0,0,0,1,0]
我怎样才能只得到计数为 Gt 0 的项目数。在这种情况下,我怎样才能只得到数字 4。
我正在使用下划线和 jQuery。
最佳答案
你可以做一个 _.reduce 来获得项目的数量 > 0:
_.reduce(items, function(m,x) { return m + (x > 0 ? 1: 0); });
关于javascript - 如何让 Underscore 的 map 只根据条件返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770792/