3 回答
TA贡献1858条经验 获得超8个赞
function trans (arr) {
let result = {nodes: []}
let depth = arr[0].length - 1
if (!arr.length || depth < 1) return obj
let cache = {
'': result.nodes
}
;[...new Array(depth).keys()].map(i => +i).forEach(i => {
arr.forEach(cur => {
let key = cur[i]
let allKey = cur.reduce((sum, cur, j) => sum += j < i ? cur : '', '')
if (i === depth - 1) {
return cache[allKey].push({name: key, value: cur[i + 1]})
}
if (!cache[allKey + key]) {
let obj = {name: key, nodes: []}
cache[allKey].push(obj)
cache[allKey + key] = obj.nodes
}
})
})
return result
}
trans(cloumns)
添加回答
举报