var list = ["a", "b", "c", "d", ] , routers = [];
var asyncRoutes = [
{ name: "a",
children: [
{ name: "b", }, { name: "c", }, { name: "d",}, { name: "e", },{ name: "f", },
],
},
];
如何遍历出来
asyncRoutes = [ { name: "a",children: [ { name: "b", }, { name: "c", }, { name: "d",} ], },] 这样的格式呢?
我是这样的写法,不能得到 children里面的数据
list.forEach((key) => {
routers.push(
...asyncRoutes.filter((item) => {
if (item.children && item.children.length > 1) {
item.children = item.children.filter((i) => {
return i.name == key;
});
}
return item.name === key;
})
);
});
js,如何将 数组进行遍历
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 黎明不管黑夜的星空 2022-08-15 10:28关注
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> var list = ["a", "b", "c", "d",], routers = []; var asyncRoutes = [ { name: "a", children: [ { name: "b", }, { name: "c", }, { name: "d", }, { name: "e", }, { name: "f", }, ], }, ]; var obj = {}; obj.children = []; var data = list.forEach((item, index) => { if (index == 0) { obj.name = item; return } var child_obj = { children: item } obj.children.push(child_obj); }); routers.push(obj) console.log(routers); </script> </body> </html>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
- ¥15 逆置单链表输出不完整
- ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
- ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
- ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
- ¥15 matlab二位可视化能否针对不同数值范围分开分级?
- ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
- ¥15 关于#极限编程#的问题,请各位专家解答!
- ¥20 win11账户锁定时间设为0无法登录
- ¥45 C#学生成绩管理系统