【问题标题】:Retrun whole path of all matching items in a nested array of objects返回嵌套对象数组中所有匹配项的完整路径
【发布时间】:2020-11-07 12:38:44
【问题描述】:

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

现在我相信,与其进一步阐明问题,代码本身在这里会更有帮助。例如,我需要在标签字段中搜索字符串“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",
                 "level":3
              },
              {
                 "label":"Lamp",
                 "key":"lamp",
                 "level":3
              }
           ]
        }
     ]
  },
  {
     "label":"Outdoor",
     "key":"outdoor",
     "level":1,
     "children":[
        {
           "label":"Furniture",
           "key":"furniture",
           "level":2,
           "children":[
              {
                 "label":"Trampoline",
                 "key":"trampoline",
                 "level":3
              },
              {
                 "label":"Swing",
                 "key":"swing",
                 "level":3
              },
              {
                 "label":"Large sofa",
                 "key":"large sofa",
                 "level":3
              },
              {
                 "label":"Medium Sofa",
                 "key":"mediumSofa",
                 "level":3
              },
              {
                 "label":"Small Sofa Wooden",
                 "key":"smallSofaWooden",
                 "level":3
              }
           ]
        },
        {
           "label":"Games",
           "key":"games",
           "level":2,
           "children":[
              
           ]
        }
     ]
  },
  {
     "label":"Refurbrished Items",
     "key":"refurbrished items",
     "level":1,
     "children":[
        
     ]
  },
  {
     "label":"Indoor",
     "key":"indoor",
     "level":1,
     "children":[
        {
           "label":"Electicity",
           "key":"electicity",
           "level":2,
           "children":[
              
           ]
        },
        {
           "label":"Living Room Sofa",
           "key":"livingRoomSofa",
           "level":2,
           "children":[
              
           ]
        }
     ]
  }
 ]
 }

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

  {
   "children":[
   // if the object or any of its children label doesn't includes 
 //sofa remove the entire object itself
  {
     "label":"Outdoor",
     "key":"outdoor",
     "level":1,
     "children":[
        {
           "label":"Indoor Furniture",
           "key":"indoorFurniture",
           "level":2,
           "children":[ // Contains "sofa", Hence retrun path to its 
            root, the object itself and all children if any. Remove unmatched siblings
              {
                 "label":"Large sofa",
                 "key":"large sofa",
                 "level":3
              },
              {
                 "label":"Medium Sofa",
                 "key":"mediumSofa",
                 "level":3
              },
              {
                 "label":"Small Sofa Wooden",
                 "key":"smallSofaWooden",
                 "level":3
              }
           ]
        }
     ]
  },
  {
     "label":"Indoor",
     "key":"indoor",
     "level":1,
     "children":[
        { // Contains "sofa", Hence retrun path to its 
            root, the object itself and all children if any. Remove 
           unmatched siblings
           "label":"Living Room Sofa",
           "key":"livingRoomSofa",
           "level":2,
           "children":[
              
           ]
        }
     ]
  }
  ]
}

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

 {
 "children":[
  {
     "label":"Home",
     "key":"home",
     "level":1,
     "children":[
        { // Contains furniture, Hence retrun path to its 
            // root, the object itself and all children if any. Remove 
           // unmatched siblings
           "label":"Indoor Furniture",
           "key":"indoorFurniture",
           "level":2,
           "children":[
              {
                 "label":"Chair",
                 "key":"chair",
                 "level":3
              },
              {
                 "label":"Table",
                 "key":"table",
                 "level":3
              },
              {
                 "label":"Lamp",
                 "key":"lamp",
                 "level":3
              }
           ]
        }
     ]
  },
  {
     "label":"Outdoor",
     "key":"outdoor",
     "level":1,
     "children":[
        { // Contains furniture, Hence retrun path to its 
            // root, the object itself and all children if any. Remove 
           // unmatched siblings
           "label":"Outdoor Furniture",
           "key":"outdoorFurniture",
           "level":2,
           "children":[
              {
                 "label":"Trampoline",
                 "key":"trampoline",
                 "level":3
              },
              {
                 "label":"Swing",
                 "key":"swing",
                 "level":3
              },
              {
                 "label":"Large sofa",
                 "key":"large sofa",
                 "level":3
              },
              {
                 "label":"Medium Sofa",
                 "key":"mediumSofa",
                 "level":3
              },
              {
                 "label":"Small Sofa Wooden",
                 "key":"smallSofaWooden",
                 "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                                                                                        
     

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

【问题讨论】:

  • 你好像问了两次同一个问题?

标签: javascript arrays search data-structures nested


【解决方案1】:

希望这是您正在寻找的:

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",
            "children": [

            ]
          },
          {
            "name": "test",
            "title": "test 6",
            "id": "test6",
            "children": [

            ]
          },
          {
            "name": "test",
            "title": "test 7",
            "id": "test7",
            "children": [

            ]
          }
        ]
      }]
    },
    {
      "name": "home 3",
      "title": "home 3",
      "id": "h3",
      "children": [{
        "name": "room 2",
        "title": "room 2",
        "id": "room2",
        "children": [{
            "name": "Dummy 5.1",
            "title": "dummy5.1",
            "id": "dummy5.1",
            "children": [

            ]
          },
          {
            "name": "test",
            "title": "test 6",
            "id": "test6",
            "children": [

            ]
          },
          {
            "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
});

【讨论】:

  • 非常感谢!这正是我想要的。
猜你喜欢
  • 2021-02-20
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
相关资源
最近更新 更多