how to filtering javascript array of objects

前端 未结 3 1505
生来不讨喜
生来不讨喜 2021-01-29 10:34

I have got two arrays . I am filtering based groupKey with PubSidebar.

3条回答
  •  无人共我
    2021-01-29 10:59

    You could take a filtering for any depth and reassemble the objects for the result.

    const
        filter = ({ content = [], ...o }) => {
            content = content.flatMap(filter);
            if (o.role !== 'public' && !groupKey.includes(o.value)) return [];
            return content.length ? { ...o, content } : o;
        },
        groupKey = ['oaDeal', 'Journals', 'Deposit'],
        pubSidebar = [{ value: 'Dashboard', role: 'public' }, { value: 'oaDeal', role: 'private', content: [{ role: 'private', value: 'oaDeal' }] }, { value: 'Journals', role: 'public', content: [{ role: 'private', value: 'Journals' }, { role: 'private', value: 'Token' }, { role: 'private', value: 'policy' }, { role: 'private', value: 'Deposit' }, { role: 'public', value: 'test' }] }],
        result = pubSidebar.flatMap(filter);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

提交回复
热议问题