有什么方法可以在数组里面的子数组加一个对象呢?

发布于 2022-09-11 18:42:08 字数 803 浏览 18 评论 0

现在有一个数组A和B

A:
[{"appNo":"aaa","descriptions":"aaa",subList:""},
{"appNo":"BBB","descriptions":"CCC",subList:[{"appNo":"ttt","menuId":"2333",menuName":"安全管理"},
{"appNo":"fff","menuId":"4444",menuName":"系统管理"}]},
{"appNo":"CCC","descriptions":"BBB",subList:""}];

B:[{"menuList":[{"appNo":"aaa","menuId":"1"},{"appNo":"aaa","menuId":"2"},{"appNo":"ttt","menuId":"3"},{"appNo":"ttt","menuId":"4"},{"appNo":"ttt","menuId":"3"},{"appNo":"ttt","menuId":"5"},{"appNo":"ttt","menuId":"6"},{"appNo":"ttt","menuId":"7"}]

B数组是一个权限配置 如果B数组中的menuList的对象的menuId等于A数组的subList的对象的menuId时 在A数组的subList的对象中加入一个{getPermit:true};
应该怎么加比较好呢 我用三次循环然后PUSH进去 一直不进去状态 是不是又更好的方法呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2022-09-18 18:42:08
const a = [
    {"appNo": "aaa", "descriptions": "aaa", subList: ""},
    {
        "appNo": "BBB",
        "descriptions": "CCC",
        subList: [{"appNo": "ttt", "menuId": "1", "menuName": "安全管理"}, {
            "appNo": "fff",
            "menuId": "4444",
            "menuName": "系统管理"
        }]
    },
    {"appNo": "CCC", "descriptions": "BBB", subList: ""}
];

const b = [
    {
        "menuList": [
            {"appNo": "aaa", "menuId": "1"},
            {"appNo": "aaa", "menuId": "2"},
            {"appNo": "ttt", "menuId": "3"},
            {"appNo": "ttt", "menuId": "4"},
            {"appNo": "ttt", "menuId": "3"},
            {"appNo": "ttt", "menuId": "5"},
            {"appNo": "ttt", "menuId": "6"},
            {"appNo": "ttt", "menuId": "7"}
        ]
    }
];

for (let i = 0; i < b.length; i++) {
    b[i].menuList.map(itemB => {
        a.map(itemA => {
            if (itemA.subList instanceof Array) {
                itemA.subList.map(itemA_S => {
                    itemA_S.menuId === itemB.menuId ? itemA['getPermit'] = true : void 0
                })
            }
        })
    })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文