SOURCE

/*
把entry转换成为如下结构
{
    a: {
        b: {
            c: {
                dd: 'abcdd'
            }
        },
        d: {
            xx: 'adxx'
        },
        e: 'ae'
    }
}
*/
const entry = {
    'a.b.c.dd': 'abcdd',
    'a.d.xx': 'adxx',
    'a.e': 'ae'
}

// const fn = (entry) => {
//     let res = {}
//     for(let key in entry){
//         let keyArr = key.split('.')
//         keyArr.reduce((acc, k, index) => {
//             if(index + 1 === keyArr.length){
//                 acc[k] = entry[key]
//                 return
//             }
//             acc[k] = acc[k] || {}
//             return acc[k]
//         }, res)
//     }
//     return res
// }

const fn = (entry) => {
    let res = {}
    for(let key in entry){
        let keyArr = key.split('.')
        const set = (curr, ks) => {
            let s_key = ks.shift()
            if(s_key){
                if(!ks.length){
                    curr[s_key] = entry[key]
                } else {
                    curr[s_key] = curr[s_key] || {}
                }
                set(curr[s_key], ks)
            }
        }
        set(res, keyArr)
    }
    return res
}

const res = fn(entry)
console.log(res)
console 命令行工具 X clear

                    
>
console