递归地在深度嵌套的对象数组中查找对象 [英] Find an object in an array of deeply nested objects recursively

查看:30
本文介绍了递归地在深度嵌套的对象数组中查找对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,每个项目都是一个具有唯一 ID 的对象.有些项目也可能有子项,它可以在子数组中包含子数组.我正在尝试使用 Id 选择一个项目.

I have an array, and each item is an object with unique Ids. Some items may also have children, and it can have children array inside a children array. I'm trying to pick an item using an Id.

           const array = [
                {
                    uuid: '40E75F3DE56B4B11B3AFBDE46785737B'
                }, {
                    uuid: '9CEF74766BBB4B9682B7817B43CEAE48'
                }, {
                    uuid: '34F209A883D3406FBA6BACD9E07DB1D9',
                    children: [{
                        uuid: 'F429C51BF01C405DA517616E0E16DE4E',
                        children: [{
                            uuid: '8823CFCE7D4645C68991332091C1A05C'
                        }, {
                            uuid: '58A9345E881F48C980498C7FFB68667D'
                        }]
                    }]
                }, {
                    uuid: '152488CC33434A8C9CACBC2E06A7E535'
                }, {
                    uuid: '9152B3DEF40F414BBBC68CACE2F5F6E4'
                }, {
                    uuid: 'B9A39766B17E4406864D785DB6893C3D'
                },
                {
                    uuid: '3J4H4J5HN6K4344D785DBJ345HSSODF',
                    children: [
                        {
                            uuid: 'EAB14DD72DA24BB88B4837C9D5276859'
                        },
                        {
                            uuid: 'FFA80D043380481F8835859A0839512B'
                        },
                        {
                            uuid: '9679687190354FA79EB9D1CA7B4962B1'
                        }
                    ]
                }
            ]

下面的代码适用于没有子元素的简单数组.我需要一个函数,或者一个可以递归搜索整个数组并返回对象(数组中的项目)的 lodash 模块

The code below works for a simple array without children. I need a function, or a lodash module that can recursively search the entire array and return the object(item in the array)

findContainingObject(array, uuid) {
    let result = [];
    result = array.filter( item => {
        return item.uuid === uuid
    })
    return result;
}

预期输出:

findContainingObject(array, '40E75F3DE56B4B11B3AFBDE46785737B')

               {
                 uuid: '40E75F3DE56B4B11B3AFBDE46785737B'
               }

findContainingObject(array, '34F209A883D3406FBA6BACD9E07DB1D9')
                {
                    uuid: '34F209A883D3406FBA6BACD9E07DB1D9',
                    children: [{
                        uuid: 'F429C51BF01C405DA517616E0E16DE4E',
                        children: [{
                            uuid: '8823CFCE7D4645C68991332091C1A05C'
                        }, {
                            uuid: '58A9345E881F48C980498C7FFB68667D'
                        }]
                    }]
                }

findContainingObject(array, '58A9345E881F48C980498C7FFB68667D')

                 {
                    uuid: '58A9345E881F48C980498C7FFB68667D'
                 }

推荐答案

该函数实现了DFS:

function findDFS(objects, id) {
  for (let o of objects || []) {
    if (o.uuid == id) return o
    const o_ = findDFS(o.children, id)
    if (o_) return o_
  }
}

和 BFS:

function findBFS(objects, id) {
  const queue = [...objects]
  while (queue.length) {
    const o = queue.shift()
    if (o.uuid == id) return o
    queue.push(...(o.children || []))
  }
}

这篇关于递归地在深度嵌套的对象数组中查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆