js 全路径还原成原本的对象?

如何根据路径对象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)

img

这两个的规则都不一样:
obj[name/name1]=11 obj[name1/name2]=22


function setValue(target, keyPath = '', value, keyPathOperator='/') {
  let keyList = keyPath.split(keyPathOperator)
  let obj = target
  
  for(let i = 0; i < keyList.length; i++) {
    let currentKey = keyList[i]
    let next = keyList[i + 1]
    
    if (next === undefined) {
      Object.assign(obj, {
        [currentKey]: value
      })
      break
    }
    
    if (!isObject(obj[currentKey])) {
      obj[currentKey] = {};
    }
    
    obj = obj[currentKey]
  }
  
  return target
}

function isObject(obj) {
  return toString.call(obj) === '[object Object]'
}

//示例
let a = {
  'b/c': 1
}

let obj = {}

setValue(obj, 'b/c', 1, '/')
console.log(obj)
// obj = {b: { c: 1 }}