首页 > 其他分享 >数组数据转化树形结构数据

数组数据转化树形结构数据

时间:2023-02-06 10:33:58浏览次数:36  
标签:parent value current 树形 parents 数组 parentId 数据 children

function translateDataToTree(data) {

​ let parents = data.filter(value => value.parentId == 'undefined' || value.parentId == null)

​ let children = data.filter(value => value.parentId !== 'undefined' && value.parentId != null)

​ let translator = (parents, children) => {

​ parents.forEach((parent) => {

​ children.forEach((current, index) => {

​ if (current.parentId === parent.id) {

​ let temp = JSON.parse(JSON.stringify(children))

​ temp.splice(index, 1)

​ translator([current], temp)

​ typeof parent.children !== 'undefined' ? parent.children.push(current) : parent.children = [current]

​ }

​ })

​ } )

​ }

​ translator(parents, children)

​ return parents

​ }

标签:parent,value,current,树形,parents,数组,parentId,数据,children
From: https://www.cnblogs.com/Daguaishou0704/p/17094640.html

相关文章