Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 搜索深嵌套_Javascript_Json_Search_Find_Underscore.js - Fatal编程技术网

Javascript 搜索深嵌套

Javascript 搜索深嵌套,javascript,json,search,find,underscore.js,Javascript,Json,Search,Find,Underscore.js,我正在开发一个解决方案,需要通过其id在深度嵌套的JSON中搜索元素。有人建议我使用下划线.js,这对我来说是相当陌生的 阅读文档后,我尝试使用find、filter和findWhere实现解决方案 下面是我使用查找尝试的内容: var test = { "menuInputRequestId": 1, "catalog":[ { "uid": 1, "name": "Pizza", "desc": "Italian

我正在开发一个解决方案,需要通过其
id
在深度嵌套的JSON中搜索元素。有人建议我使用下划线.js,这对我来说是相当陌生的

阅读文档后,我尝试使用
find
filter
findWhere
实现解决方案

下面是我使用
查找
尝试的内容:

 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;
});
和小提琴

我面临的问题是,这些函数检查结构的顶层,而不是嵌套属性,因此返回
未定义的
。我试图使用
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 ;
        });
    });
});

我不使用下划线.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,但您可以使用它

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

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

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

catalog
是一个数组,如果不使用索引ie
项,您就无法访问数组中的对象。catalog[0]。uid
因此您必须在目录上循环并检查uid。@PatrickEvans Woah。你说得对,但我试过你的建议,但还是给了我
未定义的
。理想情况下,它应该检查每个嵌套数组中的所有uid。JSON是用于数据交换的文本表示法。如果您处理的是JavaScript源代码,而不是字符串,那么您就不是在处理JSON。
catalog
是一个数组,如果不使用索引ie
item.catalog[0].uid
,则无法访问数组中的对象。因此,您必须循环遍历catalog并检查uid。@PatrickEvans-Woah。你说得对,但我试过你的建议,但还是给了我
未定义的
。理想情况下,它应该检查每个嵌套数组中的所有uid。JSON是用于数据交换的文本表示法。如果您处理的是JavaScript源代码,而不是字符串,那么您就不是在处理JSON。您好,非常感谢您的回答。。这个解决方案非常有效。如果我找不到替代方案,这将是我的解决方案+)嗨,非常感谢你的回答。。这个解决方案非常有效。如果我找不到替代方案,这将是我的解决方案+谢谢你的回答。。我查一下,然后发回:)谢谢你的回答。。我会检查并发回:)