const arrayTreeAddLevel = (array, levelName = 'level', childrenName = 'children') => {
if (!Array.isArray(array)) return []
const recursive = (array, level = 0) => {
level++
return array.map(v => {
v[levelName] = level
const child = v[childrenName]
if (child && child.length) recursive(child, level)
return v
})
}
return recursive(array)
}
版权声明:本文为weixin_41496591原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。