为了账号安全,请及时绑定邮箱和手机立即绑定

返回嵌套对象数组中所有匹配项的完整路径

返回嵌套对象数组中所有匹配项的完整路径

holdtom 2023-07-14 10:20:16
我在搜索深度嵌套的对象数组并返回所有匹配项的整个路径时遇到了非常困难的时间。我已经找到了问题的部分答案,但仅返回第一个匹配项目的路径,而我需要所有匹配项目的路径。现在我相信,与其进一步阐明问题,代码本身会更有帮助。例如,我需要在标签字段中搜索字符串“沙发”输入数据 {   "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":[             ]  },
查看完整描述

1 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

希望这就是您正在寻找的:


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

});


查看完整回答
反对 回复 2023-07-14
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信