【问题标题】:Search deep nested搜索深度嵌套
【发布时间】:2015-05-07 03:49:38
【问题描述】:

我正在研究一种解决方案,我需要通过其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 ;
        });
    });
});

【问题讨论】:

  • catalog 是一个数组,如果不使用索引,您将无法访问数组内的对象,即item.catalog[0].uid,因此您必须遍历目录并检查 uid。
  • @PatrickEvans 哇。你是对的,但我在小提琴上尝试了你的建议,但它仍然给了我undefined。理想情况下,它应该检查每个嵌套数组中的所有 uid。
  • JSON 是一种用于数据交换的文本符号(More here.) 如果你在处理 JavaScript 源代码,而不是在处理 string,那么你就不是在处理 JSON。

标签: javascript json search find underscore.js


【解决方案1】:

这是一个创建对象的解决方案,其中键是 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]

【讨论】:

  • 谢谢你的答案..我检查一下并回帖:)
【解决方案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));

【讨论】:

  • 您好,非常感谢您的回答.. 这个解决方案非常有效。如果我找不到下划线 js 替代方案,这将是我的解决方案。 +1 非常感谢 :)
猜你喜欢
  • 2018-04-13
  • 2022-06-16
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多