多级嵌套对象数据,怎么递归转成数组对象?
原数据
const obj = {
"手机": {
"华为": {
"内存": ["16g","32g","64g"],
"尺寸": ["4.8","5.2","6.8"]
}
"苹果": {
"芯片": ["A12","A13","A14"],
"内存": ["16g","32g","64g"],
"电池容量": ["3800","4000","4200"]
},
"小米": {
"内核": ["4核","6核","8核"]
}
},
"电脑": {
"联想": {
"价格": ["4500","5800","6400"],
"颜色": ["白色","黑色","银色"]
},
"华硕": {
"性能": ["一般","中等","良好"],
"显卡": ["4g","8g","10g"],
"对角比": ["12:8","16:9","16:12"]
}
}
}
想要转换成的数据
[{
title: '手机',
expand: true,
children: [
{
title: '华为',
expand: true,
children: [
{
title: '内存',
value: ["16g","32g","64g"]
},
{
title: '尺寸',
value: ["4.8","5.2","6.8"]
}
]
},
{
title: '苹果',
expand: true,
children: [
{
title: '芯片',
value: ["A12","A13","A14"]
},
{
title: '内存',
value: ["16g","32g","64g"]
},
{
title: '电池容量',
value: ["3800","4000","4200"]
}
]
},
{
title: '小米',
expand: true,
children: [
{
title: '内核',
value: ["4核","6核","8核"]
}
]
}
]
},
{
title: '电脑',
expand: true,
children: [
{
title: '联想',
expand: true,
children: [
{
title: '价格',
value: ["4500","5800","6400"]
},
{
title: '颜色',
value: ["白色","黑色","银色"]
}
]
},
{
title: '华硕',
expand: true,
children: [
{
title: '性能',
value: ["一般","中等","良好"]
},
{
title: '显卡',
value: ["4g","8g","10g"]
},
{
title: '对角比',
value: ["12:8","16:9","16:12"]
}
]
}
]
}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以不需要递归
https://segmentfault.com/q/10...
https://codepen.io/pantao/pen...
一个可行的函数: