vardataObject={"1":{"name":"第一级1","type":"第一级2","describe":"第一级3","testNum":"第一级4","1-1":{"name":"第二级5","type":"第二级6","describe":"第二级7","testNum":"第二级8","1-1-1":{"name":"第三级9","type":"第三级q","describe":"第三级w","testNum":"第三级e"},"1-1-2":{"name":"第三级r","type":"第三级ty","describe":"第三级y","testNum":"第三级y"}}},"2":{"name":"第一级6","type":"第一级d","describe":"第一级f","testNum":"第一级g"}};将里面的1-1,1-1-1,1-2之类的值修改成对应的name字段里面的值,需要替换成下面这种的:vardataObject={"第一级1":{"name":"第一级1","type":"第一级2","describe":"第一级3","testNum":"第一级4","第二级5":{"name":"第二级5","type":"第二级6","describe":"第二级7","testNum":"第二级8","第三级9":{"name":"第三级9","type":"第三级q","describe":"第三级w","testNum":"第三级e"},"第三级r":{"name":"第三级r","type":"第三级ty","describe":"第三级y","testNum":"第三级y"}}},"第一级6":{"name":"第一级6","type":"第一级d","describe":"第一级f","testNum":"第一级g"}};树形结构总共有三层,可能会有很多数据,规律就是这样,试了好久的递归,找不出解答方法,烦死了
2 回答
FFIVE
TA贡献1797条经验 获得超6个赞
functiontransform(obj){if(typeof(obj)==='object'){Object.keys(obj).forEach(key=>{if(typeof(obj[key])==='object'&&obj[key]['name']){obj[obj[key]['name']]=obj[key];obj[obj[key]['name']]=transform(obj[obj[key]['name']]);}})Object.keys(obj).forEach(key=>{if(typeof(obj[key])==='object'&&obj[key]['name']&&key!==obj[key]['name']){delete(obj[key]);}})}returnobj;}已测试,ok
添加回答
举报
0/150
提交
取消