0

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.

I need to do another condition in reduce function . For example , if Pubsidebar value is token is public, I want to keep static content {label: "test",value: "public"} without filtering with permissionObj and if other key and value is match with permissionObj,then it will be push inside token .

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',
      },
    ],
  },
  {
    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.content = c.content.filter(
        f =>
          filterObject[f.key] &&
          filterObject[f.key].some(s => s.value == f.value)
      ))
    ) {
      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)

Here is code snippet . I am trying to getting public data from sidebar without filtering with permissionObj.

my expected output would : 
[
  {
    "label": "Dashboard",
    "value": "can_view"
  },
  {
    "label": "token",
    "value": "public",
    "content": [{
        "key": "test",
        "value": "public"
      }
      {
        "key": "token",
        "value": "can_create"
      }
    ]
  },
  {
    "label": "OA deal",
    "content": [
      {
        "label": "view oadeal",
        "key": "OA deal",
        "value": "can_view"
      },
      {
        "label": "Deposit",
        "key": "Deposit",
        "value": "can_view"
      }
    ]
  },
  {
    "label": "Journals",
    "content": [
      {
        "label": "Add Journal",
        "key": "Journals",
        "value": "can_create"
      }
    ]
  }
]
13
  • your data and permissions are not stringent, for example Journal has no value and the permission calls it 'Journals'. btw, how should the nested part be filtered if no permission exists? how do you come to the result with 'Journal'? Commented Apr 6, 2020 at 9:02
  • I just edited my data Commented Apr 6, 2020 at 9:06
  • 1
    I am trying do snippet Commented Apr 6, 2020 at 10:00
  • 1
    Perfect. try to do it with static data and update your question. Commented Apr 6, 2020 at 10:04
  • 1
    can you see now my question , I did snippet Commented Apr 6, 2020 at 10:04

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.