Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Json 如何从包含Lodash的嵌套对象集合中获取图像数组?_Json_Lodash - Fatal编程技术网

Json 如何从包含Lodash的嵌套对象集合中获取图像数组?

Json 如何从包含Lodash的嵌套对象集合中获取图像数组?,json,lodash,Json,Lodash,我想从商店的“子”对象中选择所有“图像”数组。 我如何使用Lodash library做到这一点? 输出应该是一个由图像URL组成的数组: [“url1”、“url2”、“url3”]伪代码 您可以通过一点递归来解决这个问题: 抓取儿童名单 使用pulk从子列表中提取所有图像 对所有子体重复步骤1 将所有结果浓缩并展平 核心代码 演示 var数据={ “商店”:{ “家庭背景”:http://padmenu.s3.amazonaws.com/15/11/2014/05/08/2ec2ff61-

我想从商店的“子”对象中选择所有“图像”数组。 我如何使用Lodash library做到这一点? 输出应该是一个由图像URL组成的数组: [“url1”、“url2”、“url3”]

伪代码 您可以通过一点递归来解决这个问题:

  • 抓取儿童名单
  • 使用
    pulk
    从子列表中提取所有图像
  • 对所有子体重复步骤1
  • 将所有结果浓缩并展平
  • 核心代码 演示
    var数据={
    “商店”:{
    “家庭背景”:http://padmenu.s3.amazonaws.com/15/11/2014/05/08/2ec2ff61-d6a0-11e3-8857-10ddb1e6e201.jpg",
    “姓名”:{
    “tr”:“我的商店”
    },
    “菜单”:[{
    “姓名”:{
    “en”:“菜单”
    },
    “儿童”:[{
    “姓名”:{
    “en_US”:“类别”
    },
    “图像”:[
    "http://www.progressivedental-ellenlimdds.com/wp-content/uploads/2014/06/red-wine.jpg"
    ],
    “儿童”:[{
    “姓名”:{
    “en_US”:“项目”
    },
    “图像”:[
    "http://res.cloudinary.com/finedine/image/upload/c_fill,g_中心,h_600/v1435916818/WIne-Bottle_uz03a0.jpg“,
    "http://media.riepenau.com/wines/17973_b.jpg",
    "http://lorempixel.com/400/400/food/3",
    "http://lorempixel.com/400/400/food/4",
    "http://lorempixel.com/400/400/food/5",
    "http://lorempixel.com/400/400/food/6",
    "http://lorempixel.com/400/400/food/7"
    ]
    }]
    }]
    }]
    }
    };
    函数deepExtract(集合、子项、属性){
    var exists=0.negate(0.isEmpty);
    var children=\ u0.chain(collection).pull(childKey).filter(exists).flatte();
    if(u.isEmpty(children.value())){
    返回[];
    }
    var images=children.pull(property.value();
    var destinantimages=deepExtract(children.value(),childKey,property);
    返回-展平(images.concat(grages));
    };
    var-tree=\ u0.chain(data.get('shop.menus').value();
    日志(deepExtract(树、“子”、“图像”);
    //要输出到屏幕的Helper方法
    函数日志(值){
    document.getElementById(“输出”).innerHTML+=JSON.stringify(值,null,2)+“\n”
    }

    解决这个问题的最简单方法是递归地对孩子及其后代进行筛选。重点在于
    getImages()
    函数;其中,它在一个级别中展平所有子数组,提取每个图像数组并压缩所有项目以删除未定义的值(由没有图像的子数组引起),然后展平图像数组并准备进行连接。递归的停止点是当前
    子对象没有图像时,返回空数组。如果找到了图像,那么我们递归地连接所有潜在的后代图像。至于如何获得子体,我们使用与获取
    图像
    数组相同的链接序列,但使用
    子体
    作为拾取键

    function deepExtract(collection, childKey, property) {
      var exists = _.negate(_.isEmpty);
      var children = _.chain(collection).pluck(childKey).filter(exists).flatten();
    
      if (_.isEmpty(children.value())) {
        return [];
      }
    
      var images = children.pluck(property).value();
      var descendantImages = deepExtract(children.value(), childKey, property);
    
      return _.flatten(images.concat(descendantImages));
    };
    
    var tree = _.chain(data).get('shop.menus').value();
    
    var images = deepExtract(tree, 'children', 'images');
    

    我在这里找到了我问题的另一个解决方案:

    function getImages(children) {
      var images = _(children).flatten().pluck('images').compact().flatten().value();
      if(_.isEmpty(images)) {
        return [];
      }
      var descendants = _(children).flatten().pluck('children').compact().flatten().value();
      return images.concat(getImages(descendants));
    }
    
    function getShopImages(data) {
      var children = _.pluck(data.shop.menus, 'children');
      return getImages(children);
    }
    
    console.log(getShopImages(data));
    

    输出的“图像”是一个图像数组。

    您发布的JSON无效。请张贴您正在使用的正确结构。谢谢,它现在已修复@Rockstar通过
    有趣地使用
    。快速问题,如果
    shop.menu
    包含多个子菜单,这如何解决问题?或者是菜单总是包含单个元素?它总是包含单个元素(目前)。很明显,此解决方案不如您的解决方案有效,因为依赖菜单[0]不是一个好方法。我今天的解决方案是暂时的,直到有人提供了一个好的解决方案:)@Pete
    function getImages(children) {
      var images = _(children).flatten().pluck('images').compact().flatten().value();
      if(_.isEmpty(images)) {
        return [];
      }
      var descendants = _(children).flatten().pluck('children').compact().flatten().value();
      return images.concat(getImages(descendants));
    }
    
    function getShopImages(data) {
      var children = _.pluck(data.shop.menus, 'children');
      return getImages(children);
    }
    
    console.log(getShopImages(data));
    
      var children = _(shop.menus[0].children)
        .thru(function(coll) {
            return _.union(coll, _.pluck(coll, 'children'));
        })
        .flatten();
    
    var images = _.chain(children).pluck('images').flattenDeep().compact().uniq().value();