我正在使用meteor 和JavaScript 构建一个webapp,并想根据属性过滤一个JSON 对象。我可以运行fullList.find()以获取所有内容并fullList.find({'Color': 'r'})仅获取“红色”项目,但我想返回所有“红色”或“蓝色”项目,并且无法找出正确的语法。我尝试了各种组合,例如fullList.find({'Color': 'r' || 'b'})and fullList.find({'Color': ('r' || 'b')}),但它要么根本不运行,要么返回只有一个属性的项目,而不是返回带有任一属性的项目。我觉得这应该很容易解决,我只是对 JavaScript 不太熟悉。
这是我的(相关)代码以确保完整性。只有两个条件为真(而不是一或三个)的逻辑失败:
indicadores: function(){
if (Session.get("displayUrgencies")){
if (Session.get("displayInsuff")){
if (Session.get("displayStrengths")){
console.log("display all");
return fullList.find();
}
else {
console.log("display urgencies and insufficiencies");
return fullList.find({'Color': 'v' || 'r'});
}
}
else {
if (Session.get("displayStrengths")){
console.log("display urgencies and strengths");
return fullList.find({'Color': 'b' || 'r'});
}
else {
console.log("display urgencies");
return fullList.find({'Color': 'r'});
}
}
}
else if (Session.get("displayInsuff")){
if (Session.get("displayStrengths")){
console.log("display insufficiencies and strengths");
return fullList.find({'Color': 'v' || 'b'});
}
else {
console.log("display insufficiencies");
return fullList.find({'Color': 'v'});
}
}
else if (Session.get("displayStrengths")){
console.log("display strengths");
return fullList.find({'Color': 'b'});
}
else {
console.log("display all (default)");
return fullList.find();
}
},
Run Code Online (Sandbox Code Playgroud)
这个.find函数不是 JS 本身的一部分。它是 MongoDB 集合的属性。您可以简单地使用MongoDB 选择器。像这样:$or
fullList.find({
$or: [
{'Color': 'r'},
{'Color': 'b'}
]
})
Run Code Online (Sandbox Code Playgroud)
在您的示例中fullList是 MongoDB 集合。您可以在此处阅读有关它们的更多信息。
| 归档时间: |
|
| 查看次数: |
9895 次 |
| 最近记录: |