如何有效地搜索JSON数组

时间:2014-09-05 17:35:01

标签: javascript jquery arrays json

以下代码可以正常使用:

var stores = new Array();
stores[0] = { ItemNumber: "2001", Code: "001", Qty: "1", OH: "1" };
stores[1] = { ItemNumber: "2002", Code: "002", Qty: "3", OH: "2" };
stores[2] = { ItemNumber: "2003", Code: "003", Qty: "5", OH: "8" };
stores[3] = { ItemNumber: "2001", Code: "004", Qty: "10", OH: "90" };

var item = "2001";
var code = "001";

var searchResult = jQuery.grep(stores, function (store, indexInArray) {
    var found = false;
    if (item != "" && code != "") {
        found = (store.Code == code) && (store.ItemNumber == item);
    }
    else if (item != "" && code == "") {
        found = (store.ItemNumber == item);
    }
    else if (code != "" && item == "") {
        found = (store.Code == code);
    }
    return found;
});

$.each(searchResult, function (index, obj) {
    console.log("found: " + obj.Code);
});

JSFiddle http://jsfiddle.net/hwpnbkm3/

但是我对代码并不满意,因为if / else语句太多了。此外,如果我想通过数量或OH搜索,那么它将变得更加复杂。有没有办法在没有多个if / else语句的情况下搜索这个数组?我可以构建一个动态查询来搜索数组吗?

5 个答案:

答案 0 :(得分:4)

您的功能可以简化为:

return (item == "" || store.ItemNumber == item) &&
       (code == "" || store.Code == code) &&
       (item != "" || code != "");

但我建议的是:不要为查询设置单独的变量,而是创建一个对象。

var stores = new Array();
stores[0] = { ItemNumber: "2001", Code: "001", Qty: "1", OH: "1" };
stores[1] = { ItemNumber: "2002", Code: "002", Qty: "3", OH: "2" };
stores[2] = { ItemNumber: "2003", Code: "003", Qty: "5", OH: "8" };
stores[3] = { ItemNumber: "2001", Code: "004", Qty: "10", OH: "90" };

var query = { ItemNumber: "2001", Code: "001" };

var searchResult = jQuery.grep(stores, function (store, indexInArray) {
    var found = false;
    for(var n in query) {
      if(query[n] != "") {
        if(query[n] != store[n]) {
          found = false;
          break;
        }
        found = true;
      }
    }
    return found;
});

$.each(searchResult, function (index, obj) {
    console.log("found: " + obj.Code);
});

答案 1 :(得分:1)

为什么不将对象作为搜索查询传递?

var stores = new Array();
stores[0] = { ItemNumber: "2001", Code: "001", Qty: "1", OH: "1" };
stores[1] = { ItemNumber: "2002", Code: "002", Qty: "3", OH: "2" };
stores[2] = { ItemNumber: "2003", Code: "003", Qty: "5", OH: "8" };
stores[3] = { ItemNumber: "2001", Code: "004", Qty: "10", OH: "90" };

var query = {
  ItemNumber: '2001',
  Code: '004'
}

var searchResult = jQuery.grep(stores, function (store, indexInArray) {
    var matched = false;
    for (var prop in query) {
      if (store[prop] && store[prop] == query[prop]) {
        matched = true;
        break;
      }
    }
    return matched;
});

for (var matched in searchResult) {
  console.log("Item matched:" + matched);
}

请参阅http://jsfiddle.net/3axqktnn/

答案 2 :(得分:0)

你可以稍微缩短它:

var searchResult = jQuery.grep(stores, function (store, indexInArray) {
    if(typeof item !=='undefined' && item.length>0 && typeof code !=='undefined' && code.length>0)
        return store.ItemNumber==item &&store.Code==code;
    else
        return typeof item !== 'undefined' && item.length>0 ? store.ItemNumber==item : store.Code==code;
});

小提琴示例:http://jsfiddle.net/hwpnbkm3/3/

它检查两个变量,如果它们是未定义的或空字符串,那么它只会检查定义的变量。

如果您想添加更多参数,则需要传入一个对象:http://jsfiddle.net/hwpnbkm3/4/

var search={
    ItemNumber:"2001",
    Code:"AA1",
    OH:1
};

var searchResult = jQuery.grep(stores, function (store, indexInArray) {
    var found=true;
    for (var i in search){
        if(search[i]!=store[i]){
            found=false;
            break;
        }
    }
    return found;
});

我不确定您是如何设置的,但是您需要额外检查以确保搜索字段不为空并且存储字段存在。因此,您需要根据自己的设置添加一些额外的错误检查。

答案 3 :(得分:0)

我建议你改用underscorejslodash

http://underscorejs.org/#where

答案 4 :(得分:0)

var stores = new Array();
stores[0] = { ItemNumber: "2001", Code: "001", Qty: "1", OH: "1" };
stores[1] = { ItemNumber: "2002", Code: "002", Qty: "3", OH: "2" };
stores[2] = { ItemNumber: "2003", Code: "003", Qty: "5", OH: "8" };
stores[3] = { ItemNumber: "2001", Code: "004", Qty: "10", OH: "90" };

// Consider the object to be searched for.

var search = {ItemNumber: "2001", Code: "001", Qty: "1", OH: "1"};
// Based on your code above.
for(i=0;i<stores.length;i++)
{
    // iterate the objects in store, get their keys.
    var found = true;
    var keys = Object.keys(stores[i]);
    for(j =0;j<keys.length;j++)
    {   // iterate the keys and check if their value matches to the search object   
        var key = keys[j];
        var value = stores[i][key];
        found = found && (value == search[key]);
    }
   if(found) break;
}