一尘不染

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

node.js

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

这是即将形成的数据库。这是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的数据。

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数组 过滤 对象数据数组 。这是我尝试的代码。

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值匹配。然后显示数据。
我接受的输出将是这种格式:

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


  ]
 }
]

阅读 302

收藏
2020-07-07

共1个答案

一尘不染

首先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);
2020-07-07