数据处理——树形数组数据展开成一维数组,父节点在前,相应子节点尾随其后(注意判断是否存在children)

方式一:数据的map和concat方法,结合...展开运算符递归实现(推荐)

function turnTreeIntoList(array) { 
    return [].concat(...array.map(item => [].concat(item, item.children ? turnTreeIntoList(item.children) : [])))
}

上面代码如果不好理解,可以展开:

function turnTreeIntoList(array) {
    return [].concat(...(array.map(item => {
        return [].concat([item], item.children ? turnTreeIntoList(item.children) : [])
    })))
}

方式二:外部定义一个变量接收,使用forEach和concat递归实现

        let newDataArr = [];
        function turnTreeIntoList(array) {
            array.forEach(item => {
                newDataArr = newDataArr.concat([item])
                if (item.children) {
                    turnTreeIntoList(item.children)
                }
            })
            return newDataArr
        }

理论千遍不然实操一遍。准备了个数组,试一试:

树形数组:

                let list= [{
                    name: '第一项',
                    taxAmount: 12223,
                    noTaxAmount: 33,
                    warn: '超支',
                    children: [{
                        name: '第一项.1',
                        taxAmount: 123,
                        noTaxAmount: 454,
                        children: [{
                            name: '第一项.1.1',
                            taxAmount: 123,
                            noTaxAmount: 454
                        }]
                    }]
                }, {
                    name: '第二项',
                    taxAmount: 115,
                    noTaxAmount: 452124,
                    warn: '结余',
                    children: [{
                            name: '第二项.1',
                            taxAmount: 123,
                            noTaxAmount: 454
                        }
                    ]
                }, {
                    name: '第三项',
                    taxAmount: 3,
                    noTaxAmount: 43354,
                    warn: '结余',
                }]

使用上面两种方法可以得到以下数据结构:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤将一组一维数组转换树结构: 1. 定义一个空对象`map`,用于存储每个节点的信息。 2. 遍历一维数组,将每个节点的信息存储到`map`对象中,并将该节点的`id`作为`map`对象的属性名,节点信息作为该属性的值。 ``` const map = {}; for (let i = 0; i < arr.length; i++) { const node = arr[i]; map[node.id] = node; } ``` 3. 定义一个空数组`result`,用于存储树结构的根节点。 4. 遍历一维数组,对于每个节点,查找其父节点是否存在于`map`对象中。 - 如果存在,将该节点添加到其父节点的`children`数组中。 - 如果不存在,将该节点作为根节点添加到`result`数组中。 ``` const result = []; for (let i = 0; i < arr.length; i++) { const node = arr[i]; const parentId = node.parentId; if (parentId === null || parentId === undefined || !map.hasOwnProperty(parentId)) { result.push(node); } else { const parent = map[parentId]; if (!parent.children) { parent.children = []; } parent.children.push(node); } } ``` 5. 返回`result`数组即可。 完整代码如下: ``` function buildTree(arr) { const map = {}; for (let i = 0; i < arr.length; i++) { const node = arr[i]; map[node.id] = node; } const result = []; for (let i = 0; i < arr.length; i++) { const node = arr[i]; const parentId = node.parentId; if (parentId === null || parentId === undefined || !map.hasOwnProperty(parentId)) { result.push(node); } else { const parent = map[parentId]; if (!parent.children) { parent.children = []; } parent.children.push(node); } } return result; } ``` 其中,`arr`为一组一维数组,每个节点需要包含`id`和`parentId`两个属性。如果某个节点没有父节点,则其`parentId`应该为`null`或`undefined`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值