Home > Enterprise >  JavaScript filter array based on user roles
JavaScript filter array based on user roles

Time:12-31

I want to filter the array based on the user roles fetching from server. Below is the sample JSON response getting from server,

var mainObj =
  
  [
    {
      "title": "Merchant",
      "role":["0"],
      "subMenu": [
        {
          "subMenu1": "Initiate",
          "role": ["1"]
        }
      ]
    },
    {
      "title": "Prepaid",
      "role":["1"],
      "subMenu": [
        {
          "subMenu1": "Merch1",
          "role": ["2"]
        },
        {
          "subMenu1": "Merch3",
          "role": ["1"]
        }

      ]
    }
    ]

And Filter the array based on the userFlag,

var userRole = {"userFlag": 1};

I have tried to apply filter in the below but not applying correctly. can any one help on this.

const result = mainObj.filter(role => role.find(group => userRole.userFlag.includes(group.role)))

Expected output,

[
   {
      "title":"Prepaid",
      "role":[
         "1"
      ],
      "subMenu":[
         {
            "subMenu1":"Merch3",
            "role":[
               "1"
            ]
         }
      ]
   }
]

CodePudding user response:

You can try this out

var mainObj =
  
  [
    {
      "title": "Merchant",
      "role":["0"],
      "subMenu": [
        {
          "subMenu1": "Initiate",
          "role": ["1"]
        },
      ]
    },
    {
      "title": "Prepaid",
      "role":["1"],
      "subMenu": [
        {
          "subMenu1": "Merch1",
          "role": ["2"]
        },
        {
          "subMenu1": "Merch3",
          "role": ["1"]
        },

      ]
    }
    ]

    const userRole = {"userFlag": 1};
    const key = `${userRole.userFlag}`;

    function filterInner(ob){
        const res = ob.subMenu.filter(object => object.role.includes(key))
        return {...ob, subMenu: res}
    }


const returnVal = []
const result = mainObj.map(object => {
const innerfilter = filterInner(object)
      innerfilter.role.includes(key) ? returnVal.push({...object, subMenu: innerfilter.subMenu}) : null
});


console.log(returnVal)

CodePudding user response:

Given

  1. mainObj contains objects whose role value is an array of strings like ["0", "1"]
  2. userRole contains a single key userFlag whose value is a single int, like 1
  3. an object is matched when the userRole (in string form) is present in its role array

then the predicate is:

const userFlagStr = `${userRole.userFlag}`;
const result = mainObj.filter(object => object.role.includes(userFlagStr))

edit OP clarified the question to match any role in the object.

Let's build some machinery to handle this...

// return an array of roles inside the object, including the subMenu roles
const rolesIn = object => {
  const subRoles = object.subMenu.map(o => o.role).flat()
  return [...object.role, ...subRoles]
}

var mainObj = [{
    "title": "This one has no 1",
    "role": ["999"],
    "subMenu": [{
      "subMenu1": "Initiate",
      "role": ["999"]
    }]
  },
  {
    "title": "Merchant",
    "role": ["0"],
    "subMenu": [{
      "subMenu1": "Initiate",
      "role": ["1"]
    }]
  },
  {
    "title": "Prepaid",
    "role": ["1"],
    "subMenu": [{
        "subMenu1": "Merch1",
        "role": ["2"]
      },
      {
        "subMenu1": "Merch3",
        "role": ["1"]
      }

    ]
  }
]


const userRole = {
  userFlag: 1
}
const userFlagStr = `${userRole.userFlag}`;
const result = mainObj.filter(object => rolesIn(object).includes(userFlagStr))
console.log(result)

  • Related