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 = [
        {
            "Deposit": [{
                    label: "can create",
                    value: "can_create"
                }, 
            ]
        },
        {
            "Journals": [{
                label: "can create",
                value: "can_create"
            }]
        },

        {
            "Dashboard": [{
                label: "can view",
                value: "can_view"
            }]
        },
    ]

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

    const PubSidebar = [{
            label: "Dashboard",
            value: "can_view"
        }, 
        {
            label: "OA deal",
            content: [

                {
                    label: "Deposit",
                    key: "Deposit",
                    value: "can_view"
                },
                 {
                    label: "Corrections",
                    key: "Corrections",
                    value: "can_edit"
                },

            ]
        },
        {   
            label: "Journal",
            content: [{
                    label: "Add Journal",
                    key: "Journals",
                    value: "can_create"
                },

            ]
        },
    ];

Here is my PubSidebar , I need three types of filtering - if pubSidebar array of Objects then it will be filtering based on label.For examaple, Dashboard - if pubSidebar array of sub-array of objects, then filtering will be based label, key and value , For example, PermissionObj key: will be property name such as OA deal, Deposit, value : can_view or anything

My expected output would be :

    const PubSidebar = [{
            label: "Dashboard",
            value: "can_view"
        }, 
        {
            label: "OA deal",
            content: [
                {
                    label: "edit oadeal ",
                    key: "OA deal",
                    value: "can_edit"
                },

                {
                    label: "Deposit",
                    key: "Deposit",
                    value: "can_view"
                },

            ]
        },
        {   
            label: "Journal",
            content: [{
                    label: "Add Journal",
                    key: "Journals",
                    value: "can_create"
                },

            ]
        },
    ];

1 Answer 1

1

You can gain of reduce method as it allows to write complex logic and decide what should be done on each iteration of an array. I slightly edited your source data as it is hard to understand the logic of filtering.

At first, we create an object which will contain filter data. Why object? As object has O(1) to access to their keys.

const filterObject = permissionObj.reduce((a, c) => {
    for (const key in c) {
        a[key] = c[key];
    }
    return a;
},{});

Then we use reduce method to decide whether the array element is eligible to be pushed:

const result = PubSidebar.reduce((a, c)=> {
    if (filterObject[c.label] && c.value
        && filterObject[c.label].some(s => s.value == c.value) ) {
        a.push(c);
    }
    else if (c.content.some(s => filterObject[s.key]) && c.content) {
        c.content = c.content.filter(f => filterObject[f.key]
            && filterObject[f.key].some(s => s.value == f.value));
        a.push(c);
    }
    return a;
}, [])

An example:

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

const PubSidebar = [
    {
        label: "Dashboard",
        value: "can_view"
    },
    {
        label: "OA deal",
        content: [
            {
                label: "view oadeal",
                key: "OA deal",
                value: "can_view"
            },

            {
                label: "Deposit",
                key: "Deposit",
                value: "can_view"
            },
            {
                label: "Corrections",
                key: "Corrections",
                value: "can_edit"
            },

        ]
    },
    {
        label: "Journal",
        content: [{
            label: "Add Journal",
            key: "Journals",
            value: "can_create"
        },

        ]
    },
];

const filterObject = permissionObj.reduce((a, c) => {
    for (const key in c) {
        a[key] = c[key];
    }
    return a;
},{});


const result = PubSidebar.reduce((a, c)=> {
    if (filterObject[c.label] && c.value
        && filterObject[c.label].some(s => s.value == c.value) ) {
        a.push(c);
    }
    else if (c.content.some(s => filterObject[s.key]) && c.content) {
        c.content = c.content.filter(f => filterObject[f.key]
            && filterObject[f.key].some(s => s.value == f.value));
        a.push(c);
    }
    return a;
}, [])
console.log(result);

Sign up to request clarification or add additional context in comments.

10 Comments

If I want to remove child array property - { label: "view oadeal", key: "OA deal", value: "can_view" }, will the whole array remove?
Just edited again . For example , If I see OA deal , deposit empty array inside permissionobj, then OA deal remove from pubsidebar . However, if Deposit or OA deal is exists , it will be remain inside pubsidebar How can it will be done ?
@sujon it is better to do on your own as such requirements will be always and you will have to create new post at stackoverflow. So just try to write code on your own. It is really interesting. When people see your attempts, and some understandable explanation, then you will get a feedback. You are already great! I believe you can do it!:)
it's inspiration from you . thanks I am trying to do
@sujon hi! I am sorry, however, I am a little bit busy at work! I will see later
|

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.