这个方法怎么自定义children的name

这个方法怎么自定义children的name

子集的name不确定,怎么自定义children的name

export function flatTree(data, level = 0, index = 0, childrenName = 'children') {

let result = []; let obj

data.forEach(item => {

result.push(obj = {

name: item.name,

path: item.path

})

if (item.children?.length) {

const children = flatTree(item.children, level, index + 1)

if (level > index) {

obj.children = children

} else {

result = result.concat(children)

}

}

})

return result

}


回答:

function flatTree(data, level = 0, index = 0, childrenName = 'children') {

let result = []; let obj

data.forEach(item => {

result.push(obj = {

name: item.name,

path: item.path

})

if (item[childrenName]?.length) {

const children = flatTree(item[childrenName], level, index + 1,childrenName)

if (level > index) {

obj[childrenName] = children

} else {

result = result.concat(children)

}

}

})

return result

}

以上是 这个方法怎么自定义children的name 的全部内容, 来源链接: utcz.com/p/935797.html

回到顶部