指定多个子句underscore.js [英] specify more than one clause underscore.js

查看:79
本文介绍了指定多个子句underscore.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用underscore.js来过滤JSON数组。我找不到在_.where方法中组合多个子句的方法。 (ps:我正在过滤字符串属性)

I'm using underscore.js to filter a JSON array. I could not find a way to combine more than one clause in a _.where method. (ps: I'm filtering a string property)

有可能吗?

推荐答案

如您对问题的评论所述,看起来您正在尝试过滤器一个带有 OR 操作的数组 - 这是使用 _。其中无法实现的。

As described in the comments to your question, it looks like you're trying to filter an array with an OR operation - which is not possible using _.where.

使用 _。过滤器代替:

var arr = [{ x:1, y:2 }, { x:2, y:1 }, { x:3, y:3 }];
var result = _.filter(arr, function(obj) {
     // return true for every valid entry!
     return obj.x == 1 || obj.y == 1;
});
console.log(result); // [{ x:1, y:2 },{ x:2, y:1 }]

小提琴

这篇关于指定多个子句underscore.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆