js实现树形数据结构

转发自:https://blog.csdn.net/Mr_JavaScript/article/details/82817177

// 调用时,字段名以字符串的形式传参,如treeData(source, 'id', 'parentId', 'children')
function treeData(source, id, parentId, children){   
    let cloneData = JSON.parse(JSON.stringify(source))
    return cloneData.filter(father=>{
        let branchArr = cloneData.filter(child => father[id] == child[parentId]);
        branchArr.length>0 ? father[children] = branchArr : ''
        return father[parentId] == 0        // 如果第一层不是parentId=0,请自行修改
    })
}

 

 

你可能感兴趣的:(js实现树形数据结构)