-1

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.

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_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: 'transction',
value:"public",content:[{label:"test2",value:"public"}]

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

I am trying to getting public data from sidebar without filtering with permissionObj.

my expected output would :

    [
      {
        "label": "Dashboard",
        "value": "can_view"
      },
{
            label: 'transction',
    value:"public",content:[{label:"test2",value:"public"}]

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

2 Answers 2

1

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)

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

7 Comments

if I remove can_create from token , I don't see other property
@sujon because your token key of permissionObj has value can_create. So filter method works
I want to constant token in pubSidebar , sub-array property will be add/remove based on permissionobj .
{ label: 'token', value: 'public', content: [ { key: 'test', value: 'public', }, ], }, this will be constent , but token will be add/remove based on token
@sujon As a good practice, you need to write all conditions before post question. After people answered, it is not ok to change your requirements as it is new effort of people who answered to you. In my view, you need to create new question or show your efforts
|
0

I tried this with the object you provided in the question (permissionObj)

permissionObj.map( 
  p =>{  
    return { 
      label: Object.keys(p)[0], value:p[Object.keys(p)[0]][0].value 
    }
  }
)

which walks the keys, and take the first as the label and its first sub array object to get the value

resulting in this:

[{"label":"OA deal","value":"can_view"},{"label":"Deposit","value":"can_view"},{"label":"Journals","value":"can_create"},{"label":"Dashboard","value":"can_view"},{"label":"token","value":"can_create"}]

some of the data you required isnt in the permissionObj so I cannot add that - as i dont know where that comes from

3 Comments

I didn't understand your concept , you add snippet ?
in your question you have permissionObj - i am walking this to get part of the output - the rest of your output isnt in the object you provided - so i cant get that
you can achieve my expected output

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.