如何在JavaScript中将对象数组过滤为另一个对象数组?

如何在JavaScript中将对象数组过滤为另一个对象数组?,第1张

如何在JavaScript中将对象数组过滤为另一个对象数组?

首先

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);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5505937.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存