搜索深层嵌套

时间:2015-05-07 03:49:38

标签: javascript json search find underscore.js

我正在开发一个解决方案,我需要通过其id在深层嵌套的JSON中搜索元素。我被建议使用 underscore.js ,我很陌生。

阅读文档http://underscorejs.org/#find后,我尝试使用findfilterfindWhere来实施解决方案。

以下是我尝试使用find

的内容
 var test = {
    "menuInputRequestId": 1,
    "catalog":[
      {
        "uid": 1,
        "name": "Pizza",
        "desc": "Italian cuisine",
        "products": [
          {
            "uid": 3,
            "name": "Devilled chicken",
            "desc": "chicken pizza",
            "prices":[
              {
                "uid": 7,
                "name": "regular",
                "price": "$10"
              },
              {
                "uid": 8,
                "name": "large",
                "price": "$12"
              }
            ]
          }
        ]
      },
      {
        "uid": 2,
        "name": "Pasta",
        "desc": "Italian cuisine pasta",
        "products": [
          {
            "uid": 4,
            "name": "Lasagne",
            "desc": "chicken lasage",
            "prices":[
              {
                "uid": 9,
                "name": "small",
                "price": "$10"
              },
              {
                "uid": 10,
                "name": "large",
                "price": "$15"
              }
            ]
          },
          {
            "uid": 5,
            "name": "Pasta",
            "desc": "chicken pasta",
            "prices":[
              {
                "uid": 11,
                "name": "small",
                "price": "$8"
              },
              {
                "uid": 12,
                "name": "large",
                "price": "$12"
              }
            ]
          }
        ]
      }
    ]
  };

var x = _.find(test, function (item) {
    return item.catalog && item.catalog.uid == 1;
});

小提琴http://jsfiddle.net/8hmz0760/

我遇到的问题是这些函数检查结构的顶层而不是嵌套属性,因此返回undefined。我尝试使用类似问题Underscore.js - filtering in a nested Json中建议的item.catalog && item.catalog.uid == 1;逻辑,但失败了。

如何通过搜索整个深层嵌套结构来按值找到项目?

修改

以下代码是我尝试的最新代码。问题在于它直接遍历价格嵌套对象并试图找到该值。但我的要求是在JSON的所有层中搜索值。

var x = _.filter(test, function(evt) {
    return _.any(evt.items, function(itm){
        return _.any(itm.outcomes, function(prc) {
            return prc.uid === 1 ;
        });
    });
});

2 个答案:

答案 0 :(得分:2)

我不使用underscore.js但你可以使用它来代替

function isArray(what) {
return Object.prototype.toString.call(what) === '[object Array]';
}

function find(json,key,value){
var result = [];
for (var property in json)
{
    //console.log(property);
    if (json.hasOwnProperty(property)) {
        if( property == key && json[property] == value)
        {
            result.push(json);
        }
        if( isArray(json[property]))
        {
            for(var child in json[property])
            {
                //console.log(json[property][child]);
                var res = find(json[property][child],key,value);
                if(res.length >= 1 ){
                    result.push(res);}
            }
        }
    }
}
return result;
}

console.log(find(test,"uid",4));

答案 1 :(得分:2)

这是一个创建一个对象的解决方案,其中键是uid:

var catalogues = test.catalog;
var products = _.flatten(_.pluck(catalogues, 'products'));
var prices = _.flatten(_.pluck(products, 'prices'));

var ids = _.reduce(catalogues.concat(products,prices), function(memo, value){
  memo[value.uid] = value;
  return memo;
}, {});

var itemWithUid2 = ids[2]
var itemWithUid12 = ids[12]