如何在JavaScript中将对象数组过滤为另一个对象数组? [英] How to filtering array of objects to another array of objects in javascript?

查看:1342
本文介绍了如何在JavaScript中将对象数组过滤为另一个对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象数组.我想基于PermissionObj过滤数据.

I have got two arrays of objects . I want to filtering data based on PermissionObj.

这是即将到来的表单数据库.这是permissionObj中的子数组的数组.

This is coming form database. Here is arrays of sub-arrays in the permissionObj .

    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"
        },
       ]
      }
     ]
    }

这是静态数据.我想根据PermissionObj比较这些数据.

this is static data. I want to compare this data based on PermissionObj.

const data = [{
  label: "books",
  value: "can_view"
 },
 {
  label: "deal",
  content: [{

   value: "can_update"
  }, {
   value: "can_delete"
  },{value:"can_view"}]
 },
 {
  label: "Articles",

 },
 {
  label: "Journals",
  content: [{
    value: "can_create"
   },
   {
    value: "can_view"
   },
  {
    value: "can_delete"
   },
 {
    value: "can_edit"
   },

  ]
 }
]

我正在尝试根据 PermissionObj对象数组过滤对象的数据数组.这是我尝试的代码.

I am trying filter data array of object based on PermissionObj array of objects . here is my trying code.

let permission = PermissionObj.permission 

permission.filter(item=>{
  // I am finding data label by using permission label.

 let dataItem = data.find(index=>item[index.label])

 console.log(dataItem)

})

  // the problem is that I want to filtering based on value .

  // if both of value is true , then show the data . 

如果PermissionObj值将与data值匹配.然后显示数据.
我接受的输出将是以下格式:

if PermissionObj value will be matched with data value . then show the data .
My accepted Output would be this format :

const data = [{
  label: "books",
  value: "can_view"
 },
 {
  label: "deal",
  content: [{
   value: "can_update"
  }, {
   value: "can_delete"
  }]
 },
 {
  label: "Journals",
  content: [{
    value: "can_create"
   },
   {
    value: "can_view"
   },


  ]
 }
]

推荐答案

首先从PermissionObj构建keys.在标签包含在keys中的数据数组上使用过滤器.

First build keys from PermissionObj. Use filter on data array which label includes in 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);

这篇关于如何在JavaScript中将对象数组过滤为另一个对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆