How to filtering array of objects to another array of objects in javascript?

I have got two arrays of objects . I want to filtering data based on 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"
        },
       ]
      }
     ]
    }

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"
  }]
 },
 {
  label: "Articles",

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


  ]
 }
]

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 . 

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


  ]
 }
]

Did you do curriculum here on freeCodeCamp? It has very similar challenge:

Design-wise, it’s fairly bad practice to filter stuff after it came from database, the thing you wanna do is to determine what information user has rights to access before and ask database only to give you that specific information. Also needless to say that logic determining it should never run on the front-end.

1 Like

I did not do on freecodecamp . can you solve theproblem

Then you need to make your logic other way around, filter data based on permission, right?

1 Like

yes , I want to accepted data based permission object

Yes, but in this example you do the opposite. You seemed to filter permissions and for each permission to lookup for the entry in the data. Try to invert that

1 Like

can you give me solution?

const permKeys = PermissionObj.permission.flatMap(item => Object.keys(item));

const filteredData = data.filter(({ label }) => permKeys.includes(label));

console.log(filteredData);

Here is my problem , I have been faced is that I don’t want to get can_edit , can_delete if it doesn’t match with permission objects in journals. In my permission objects , There is no can_edit and can_delete in journals .

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


  ]
 }
]

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile: