如何根据路径对象obj[name/name1]=11 obj[name/name2]=22 obj[age]=33,还原成原本对象
let message={name:{name1:11,name2:22},age:33}
有没有知道的告诉一下
如何根据路径对象obj[name/name1]=11 obj[name/name2]=22 obj[age]=33,还原成原本对象
let message={name:{name1:11,name2:22},age:33}
有没有知道的告诉一下
let obj = {}
obj['name/name1'] = 11
obj['name/name2'] = 22
obj['age'] = 33
let result = Object.keys(obj).reduce((total, item) => {
if (item.indexOf('/') == -1) {
total[item] = obj[item]
} else {
let strings = item.split('/');
if (!total[strings[0]]) {
total[strings[0]] = {};
}
total[strings[0]][strings[1]] = obj[item]
}
return total
}, {});
console.log(result)