微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重排嵌套对象数组中所有匹配项的整个路径

如何解决重排嵌套对象数组中所有匹配项的整个路径

我在搜索深层嵌套的对象数组并返回所有匹配项的整个路径时非常费劲。我已经找到问题的部分答案,但是它只返回第一个匹配项的路径,而我需要所有匹配项的路径。

现在,我相信与其对问题进行进一步的阐述,在这里代码本身会更有帮助。例如,我需要在标签字段中搜索字符串“ sofa”

输入数据

 {
   "children":[
  {
     "label":"Home","key":"home","level":1,"children":[
        {
           "label":"Furniture","key":"furniture","level":2,"children":[
              {
                 "label":"Chair","key":"chair","level":3
              },{
                 "label":"Table","key":"table",{
                 "label":"Lamp","key":"lamp","level":3
              }
           ]
        }
     ]
  },{
     "label":"Outdoor","key":"outdoor","children":[
              {
                 "label":"Trampoline","key":"trampoline",{
                 "label":"Swing","key":"swing",{
                 "label":"Large sofa","key":"large sofa",{
                 "label":"Medium Sofa","key":"mediumSofa",{
                 "label":"Small Sofa Wooden","key":"smallSofaWooden","level":3
              }
           ]
        },{
           "label":"Games","key":"games","children":[
              
           ]
        }
     ]
  },{
     "label":"Refurbrished Items","key":"refurbrished items","children":[
        
     ]
  },{
     "label":"Indoor","key":"indoor","children":[
        {
           "label":"Electicity","key":"electicity","children":[
              
           ]
        },{
           "label":"Living Room Sofa","key":"livingRoomSofa","children":[
              
           ]
        }
     ]
  }
 ]
 }

**预期输出-搜索沙发**

  {
   "children":[
   // if the object or any of its children label doesn't includes 
 //sofa remove the entire object itself
  {
     "label":"Outdoor","children":[
        {
           "label":"Indoor Furniture","key":"indoorFurniture","children":[ // Contains "sofa",Hence retrun path to its 
            root,the object itself and all children if any. Remove unmatched siblings
              {
                 "label":"Large sofa","children":[
        { // Contains "sofa",the object itself and all children if any. Remove 
           unmatched siblings
           "label":"Living Room Sofa","children":[
              
           ]
        }
     ]
  }
  ]
}

**预期产出-搜索家具**

 {
 "children":[
  {
     "label":"Home","children":[
        { // Contains furniture,Hence retrun path to its 
            // root,the object itself and all children if any. Remove 
           // unmatched siblings
           "label":"Indoor Furniture",the object itself and all children if any. Remove 
           // unmatched siblings
           "label":"Outdoor Furniture","key":"outdoorFurniture","level":3
              }
           ]
        }
     ]
  }
  ]

}

我尝试过的事情

function findChild(obj,condition) {
if (Object.entries(condition).every( ([k,v]) => obj[k] === v )) {
    return obj;
}
for (const child of obj.children || []) {
    const found = findChild(child,condition);
    // If found,then add this node to the ancestors of the result
    if (found) return Object.assign({},obj,{ children: [found] });
 }
}
var search = { label: 'sofa' };
console.log(findChild(input,search)); 
//https://stackoverflow.com/a/48501349/10414881
// it return only the first matched item,but i need all of the 
 items                                                                                        
     

非常感谢您的帮助。我真的被困在这里。请帮忙。

解决方法

希望这就是您想要的:

const input = {
  "children": [{
      "name": "test","title": "test 1","id": "t1","children": [

      ]
    },{
      "name": "test","title": "test 2","id": "t2","children": [{
          "name": "Dummy 1","title": "dummy","id": "dummy1","children": [

          ]
        },{
          "name": "Dummy 2","title": "dummy2","id": "dummy2","children": [{
            "name": "Dummy 2.1","title": "dummy2.1","id": "dummy2.1","children": [

            ]
          }]
        }
      ]
    },{
      "name": "home 1","title": "home 1","id": "h1","children": [{
        "name": "room 1","title": "room 1","id": "room1","children": [{
            "name": "Dummy 4.1","title": "dummy4.1","id": "dummy4.1","children": [

            ]
          },{
            "name": "test","title": "test 4","id": "test4","children": [

            ]
          }
        ]
      }]
    },{
      "name": "home 2","title": "home 2","id": "h2","children": [{
        "name": "room 2","title": "room 2","id": "room2","children": [{
            "name": "Dummy 5.1","title": "dummy5.1","id": "dummy5.1","title": "test 6","id": "test6","title": "test 7","id": "test7",{
      "name": "home 3","title": "home 3","id": "h3",{
            "name": "computer","title": "computer1","id": "computer1","children": [{
                "name": "Dummy 7.1","title": "dummy7.1","id": "dummy7.1","children": [

                ]
              },{
                "name": "test","title": "test 10","id": "test10","children": [

                ]
              }
            ]
          }
        ]
      }]
    }
  ]
}

function findChild(obj,condition) {
  if (Object.entries(condition).every(([k,v]) => obj[k] === v)) {
    return obj;
  }
  const children = []
  if (obj.children.length > 0) {
    for (const child of obj.children) {
      const found = findChild(child,condition);
      // If found,then add this node to the ancestors of the result
      if (found)
        children.push(found);
    }
    obj.children = children;
    return obj;
  }
  return null;
}

var search = {
  name: 'test'
};
console.dir(findChild(input,search),{
  depth: null
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。