1

I have got two arrays of objects. I want to filter data based on permissionObj.

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

   let permissionObj = [{
            "OA deal": [{
                    label: "can view",
                    value: "can_view"
                }, {
                    label: "can create",
                    value: "can_create"
                },
                {
                    label: "can edit",
                    value: "can_edit"
                }, {
                    label: "can delete",
                    value: "can_delete"
                }
            ]
        },
        {
            "Deposit": [{
                    label: "can view",
                    value: "can_view"
                }, {
                    label: "can create",
                    value: "can_create"
                },
                {
                    label: "can edit",
                    value: "can_edit"
                }, {
                    label: "can delete",
                    value: "can_delete"
                }
            ]
        },

        {
            "Journals": [{
                label: "can view",
                value: "can_view"
            }, {
                label: "can create",
                value: "can_create"
            }, {
                label: "can edit",
                value: "can_edit"
            }, {
                label: "can delete",
                value: "can_delete"
            }]
        },
        {
            "Deposit": [{
                label: "can view",
                value: "can_view"
            }, {
                label: "can create",
                value: "can_create"
            }, {
                label: "can edit",
                value: "can_edit"
            }, {
                label: "can delete",
                value: "can_delete"
            }]
        },


        {
            "OA deals request": []
        },
        {
            "Deposit Approval": []
        },
        {
            "Dashboard": [{
                label: "can view",
                value: "can_view"
            }, {
                label: "can create",
                value: "can_create"
            }, {
                label: "can edit",
                value: "can_edit"
            }, {
                label: "can delete",
                value: "can_delete"
            }]
        },
    ]

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

    console.log(permission)
    const PubSidebar = [{
            label: "Dashboard",
            key: "Dashboard",
            value: "can_view"
        }, {
            label: "Settings",
            key: "Settings",
            value: "can_view"
        },
        {
            label: "OA deal activities",
            key:"OA Deal" || "Deposit"|| "Corrections" ,
            content: [{
                    label: "Add oadeal",

                    key: "OA deal",
                    value: "can_create"
                },

                {
                    label: "edit oadeal ",

                    key: "OA deal",
                    value: "can_edit"
                },

                {

                    label: "view oadeal ",
                    key: "OA deal",
                    value: "can_view"

                },

                {
                    label: "Deposit for view",
                    key: "Deposit",
                    value: "can_view"
                },
                {
                    label: "Deposit for edit",
                    key: "Deposit",
                    value: "can_edit"

                },
                {
                    key: "Deposit",
                    value: "can_edit"

                },
                {
                    label: "edit Corrections",
                    key: "Corrections",
                    value: "can_edit"

                },
                {
                    label: "Add Corrections",
                    key: "Corrections",
                    value: "can_create"

                },

                {
                    label: "view Corrections",
                    key: "Corrections",
                    value: "can_view"

                }
            ]
        },
        { key:"Journals"
            label: "Journal for publishers",
            content: [{
                    label: "Add Journal",
                    key: "Journals",
                    value: "can_create"
                },
                {
                    label: "Journals List",
                    key: "Journals",
                    value: "can_view"

                },

            ]
        },
    ];

I am trying to filter the PubSidebar array of the object based on permissionObj array of objects. here is my trying code.

 let permission =
      permissionObj.filter(
        x => Object.values(x)[0].length !== 0
      );

    const result =
      PubSidebar &&
      PubSidebar.reduce((accumulator, currentvalue) => {
        let filterObj = permission && permission.find(f => f[currentvalue.key]);
        // console.log("filterObj", filterObj);
        if (filterObj) {
          if (currentvalue.value) {
            accumulator.push(currentvalue);
          }
          if (currentvalue.content) {
            console.log(currentvalue);
            currentvalue.content =
              currentvalue &&
              currentvalue.content &&
              currentvalue.content.filter(
                f =>
                  filterObj[currentvalue.key] &&
                  filterObj[currentvalue.key].some(
                    s =>
                      s.value &&
                      s.value.toLowerCase() &&
                      s.value === f.value.toLowerCase()
                  )
              );
            accumulator.push(currentvalue);
          }
        }
        return accumulator;
      }, []);

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 corrections. In my permission objects, There is no Corrections.

I want to filtering pubsidebar based on key and value of permissionObj objects. It will be this format value

    // My Accepted Output would be :


    const output = [{
            label: "Dashboard",
            key: "Dashboard",
            value: "can_view"
        },
        {   key:"OA Deal" || "Deposit",
            label: "OA deal activities",
            content: [{
                    label: "Add oadeal",
                    key: "OA deal",
                    value: "can_create"
                },

                {
                    label: "edit oadeal ",
                    key: "OA deal",
                    value: "can_edit"
                },

                {
                    label: "view oadeal ",
                    key: "OA deal",
                    value: "can_view"

                },

                {
                    label: "Deposit for view",
                    key: "Deposit",
                    value: "can_view"
                },
                {
                    label: "Deposit for edit",
                    key: "Deposit",
                    value: "can_edit"

                },
                {
                    key: "Deposit",
                    value: "can_edit"

                },


            ]
        },
        { key:"Journals"
            label: "Journal for publishers",
            content: [{
                    label: "Add Journal",
                    key: "Journals",
                    value: "can_create"
                },
                {
                    label: "Journals List",
                    key: "Journals",
                    value: "can_view"

                },

            ]
        },
    ];
7
  • waiting , I am writing full question again Commented Mar 30, 2020 at 16:24
  • I edited my question . can you see Commented Mar 30, 2020 at 17:01
  • if my items are coming with sub-arrays , I want to filter with key and value , my key will be permission property name and value will be can_view, can_create and anything . I want to match and return data Commented Mar 30, 2020 at 17:03
  • 1
    Your permissionObj does not have OA deal activities. What's rule to have OA deal activities in result array? It would be great if you have less data in your examples. It is really hard to understand what it is going on for me. Thanks. Commented Mar 30, 2020 at 17:12
  • I just edit again , I added key by using OR operator below oa deal activities because , I want to add inside OA Deal activities based on parent key Commented Mar 30, 2020 at 17:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.