首先
keys从PermissionObj 构建。在标签包含的数据数组上使用过滤器
keys。
const PermissionObj = { permission: [ { books: [ { label: "Can View", value: "can_view" } ] }, { Journals: [ { label: "Can View", value: "can_view" }, { label: "Can Create", value: "can_create" } ] }, { deal: [ { label: "Can update", value: "can_update" }, { label: "Can delete", value: "can_delete" } ] } ]};const data = [ { label: "books", value: "can_view" }, { label: "deal", content: [ { value: "can_update" }, { value: "can_delete" } ] }, { label: "Articles" }, { label: "Journals", content: [ { value: "can_create" }, { value: "can_view" } ] }];const perm = {};PermissionObj.permission.forEach(item => Object.entries(item).forEach(([key, values]) => { perm[key] = values.map(x => x.value); }));const updated = data .map(item => { const newItem = { ...item }; if (item.content) { newItem.content = item.content.filter(x => perm[item.label].includes(x.value) ); } else if (item.value) { newItem.value = perm[item.label].includes(item.value) ? item.value : ""; } return newItem; }) .filter(x => x.content || x.value);console.log(updated);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)