我已存储了一些数据,如下所示
var store = new Memory({
data: [
{id: 1, name: "one", prime: false },
{id: 2, name: "two", even: true, prime: true},
{id: 3, name: "three", prime: true},
{id: 4, name: "four", even: true, prime: false},
{id: 5, name: "five", prime: true}
]
});
我可以查询" prime"如下所示。
var results = store.query({ prime: true });
但是我怎样才能得到名字""是一个"一个"或者"两个"不使用foreach查询结果如下
var results = store.query().forEach(function(each){
if(each.name == "one" || each.name == "two") {
console.log(each)
}
})
答案 0 :(得分:1)
是的,您可以对同一字段的多个值执行查询:
regex
传递函数:
require([
"dojo/store/Memory",
"dojo/domReady!"
], function(Memory) {
var store = new Memory({
data: [
{id: 1, name: "one", prime: false },
{id: 2, name: "two", even: true, prime: true},
{id: 3, name: "three", prime: true},
{id: 4, name: "four", even: true, prime: false},
{id: 5, name: "five", prime: true}
]
});
results = store.query({ name: new RegExp("one|two")});
console.log(results);
});
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.11.2/dojo/dojo.js"></script>
require([
"dojo/store/Memory",
"dojo/domReady!"
], function(Memory) {
var store = new Memory({
data: [
{id: 1, name: "one", prime: false },
{id: 2, name: "two", even: true, prime: true},
{id: 3, name: "three", prime: true},
{id: 4, name: "four", even: true, prime: false},
{id: 5, name: "five", prime: true}
]
});
var results = store.query(
function (element) {
return element.name == "one" || element.name == "two";
}
);
console.log(results);
});
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.11.2/dojo/dojo.js"></script>