how to filter array of object and nested of array

后端 未结 2 634
时光说笑
时光说笑 2021-01-29 06:32

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

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

2条回答
  •  清酒与你
    2021-01-29 06:48

    You can add a condition inside of reduce method and your content property will not be filtered:

    if (c.value ==='public' && c.content.some(f => filterObject[f.key] && 
        filterObject[f.key].some(s => s.value == f.value))) {
            a.push(c);
    }
    

    And try to avoid filter your data inside of if condition.

    So the code would look like this:

    let permissionObj = [
      {
        'OA deal': [
          {
            label: 'can view',
            value: 'can_view',
          },
        ],
      },
    
      {
        Deposit: [
          {
            label: 'can_view',
            value: 'can_view',
          },
        ],
      },
    
      {
        Journals: [
          {
            label: 'can create',
            value: 'can_create',
          },
        ],
      },
      {
        Dashboard: [
          {
            label: 'can view',
            value: 'can_view',
          },
        ],
      },
      {
        token: [
          {
            label: 'can view',
            value: 'can_create',
          },
        ],
      },
    ]
    
    const PubSidebar = [
      {
        label: 'Dashboard',
        value: 'can_view',
      },
      {
        label: 'token',
        value: 'public',
        content: [
          {
            key: 'token',
            value: 'can_create',
          },
          {
            key: 'test',
            value: 'public',
          },
        ],
      },
      {
        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: 'Journals',
        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.value === 'public' && c.label === 'token') {
          if (c.value ==='public' && c.content.some(f => filterObject[f.key] && filterObject[f.key].some(s => s.value == f.value))) {
              a.push(c);
          }
          else {
              c.content = c.content.filter(f => filterObject[f.key]
                  &&  filterObject[f.key].some(s => s.value == f.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)

提交回复
热议问题