const entry = {
a: {
b: {
c: {
dd: 'abcdd'
}
},
d: {
xx: 'adxx'
},
e: 'ae'
}
}
// 要求转换成如下对象
// var output = {
// 'a.b.c.dd': 'abcdd',
// 'a.d.xx': 'adxx',
// 'a.e': 'ae'
// }
//递归
function flatObj( obj, parentKey= '', result= {}) {
Object.keys(obj).forEach(key => {
let increasedKey = parentKey == "" ? `${parentKey}${key}` :`${parentKey}.${key}`
if( typeof obj[key] == "object") {
flatObj(obj[key], increasedKey, result)
}else {
result[increasedKey] = obj[key]
}
})
return result
}
const output = flatObj(entry)
Object.keys(output).forEach(key => {
console.log(key+':'+output[key])
})
console