如何在javascript中过滤对象的子数组和对象的子数组 [英] how to filtering array of sub-array of objects and array of sub-array of objects in javascript

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

问题描述

我有两个对象数组。我想根据PermissionObj筛选数据。

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

            ]
        },
    ];

这是我的PubSidebar,我需要三种类型的过滤
-如果pubSidebar对象数组例如,仪表板
-如果pubSidebar对象子数组的数组,则过滤将基于label,key和value,例如PermissionObj key:将是属性名称,例如作为OA交易,存款,价值:can_view或其他任何东西

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

我的预期输出为:

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

            ]
        },
    ];


推荐答案

您可以减少 方法,因为它允许编写复杂的逻辑并确定在数组的每次迭代中应执行的操作。我对您的源数据进行了稍微的编辑,因为很难理解过滤的逻辑。

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.

首先,我们创建一个包含过滤数据的对象。为什么反对?由于对象具有 O(1)来访问其键。

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;
},{});

然后我们使用 reduce 方法确定是否

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;
}, [])

示例:

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);

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

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