You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修正JavaScript递归逻辑,获取树结构中check为true的节点ID扁平数组

递归获取树结构中check为true的节点ID并生成扁平数组

我有如下对象树结构数据,想要获取所有check属性为true的节点id,并存入扁平数组。尝试用递归实现,但得到的是嵌套数组,不符合预期。

树结构数据

[
    {
        "id": "1000",
        "parentId": "-1",
        "check": true,
        "children": [
            {
                "id": "2000",
                "parentId": "1000",
                "check": true,
                "children": [
                    {
                        "id": "3000",
                        "parentId": "2000",
                        "check": true
                    },
                    {
                        "id": "3001",
                        "parentId": "2000",
                        "check": true
                    },
                    {
                        "id": "3002",
                        "parentId": "2000",
                        "check": false
                    }
                ]
            }
        ]
    },
    {
        "id": "1001",
        "parentId": "-1",
        "check": true,
        "children": [
            {
                "id": "2001",
                "parentId": "1001",
                "check": true,
                "children": [
                    {
                        "id": "3003",
                        "parentId": "2001",
                        "check": true
                    },
                    {
                        "id": "3004",
                        "parentId": "2001",
                        "check": true
                    },
                    {
                        "id": "3005",
                        "parentId": "2001",
                        "check": true
                    }
                ]
            }
        ]
    }
]

当前递归函数(返回嵌套数组)

function getCheck(arr) {
    let result = []
    arr.forEach(item => {
        if (item.check) {
            result.push(item.id)
        }
        if (item.children) {
            let child = getCheck(item.children)
            if (child.length) {
                result.push(child) // 此处直接push数组导致嵌套
            }
        }
    })
    return result
}

期望得到的扁平ID数组示例:["1000","2000","3000","3001","1001","2001","3003","3004","3005"]


修改后的代码方案

问题根源是递归调用时,你把返回的子数组作为单个元素push到结果数组中,导致嵌套。需要将子数组的元素逐个合并到结果数组,以下是几种可行方案:

方案一:使用扩展运算符 ...

function getCheck(arr) {
    let result = []
    arr.forEach(item => {
        if (item.check) {
            result.push(item.id)
        }
        if (item.children) {
            const childIds = getCheck(item.children)
            // 展开子数组元素后添加到结果
            result.push(...childIds)
        }
    })
    return result
}

方案二:使用 Array.concat()

function getCheck(arr) {
    let result = []
    arr.forEach(item => {
        if (item.check) {
            result.push(item.id)
        }
        if (item.children) {
            // 合并当前结果与子节点递归结果
            result = result.concat(getCheck(item.children))
        }
    })
    return result
}

方案三:简洁的reduce写法

function getCheck(arr) {
    return arr.reduce((acc, item) => {
        if (item.check) acc.push(item.id)
        if (item.children) acc.push(...getCheck(item.children))
        return acc
    }, [])
}

三种方案都能生成目标扁平数组,核心是避免嵌套数组的直接插入,改为合并子数组的元素到结果中。


内容的提问来源于stack exchange,提问作者Old Letter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:15:39