Dojo Store内存查询

时间:2016-09-07 04:46:45

标签: memory dojo store

我已存储了一些数据,如下所示

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)
    }
})

1 个答案:

答案 0 :(得分:1)

是的,您可以对同一字段的多个值执行查询:

  1. 不使用regex传递函数:
  2. 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>

    Fiddle

    1. 对于复杂查询,将具有查询搜索条件的函数作为参数传递,如下所示:
    2. 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>

      Fiddle