1

This is the JSON for user data.

result = {
  "id": 5529,
  "roles": [
    {
      "id": 10,
      "name": "ADMIN",
      "privileges": [
        {
          "id": 100,
          "name": "ADD",
          "systemId": 3
        },
        {
          "id": 115,
          "name": "DEL",
          "systemId": 3
        }
      ]
    }
  ]
}

Have to check the given privilege value exist in the List and return true or false.

Tried few codes but not working as expected, one such code tried is given below. Here privilege = DEL, then it should return true.

result.roles.forEach((element:any) => {
                   element.privileges.filter(function(item:any){
            if(item.name === privilege)
                return true;
            else 
                return false;
            });

How can I check arrays inside an array efficiently and filter based on a value and return the result.

2
  • BTW, instead if(item.name === privilege) return true; else return false; }); you can write just return item.name === privilege; Commented Aug 29, 2017 at 6:15
  • Its not returning any value. Commented Aug 29, 2017 at 6:24

3 Answers 3

3

While forEach function operates on the original array, Filter function doesn't. It gives the filtered array as output. So you are filtering the data inside alright but it is not getting assigned to the actual array.

Try this,

result.roles.forEach((element) => {
    element.privileges = element.privileges.filter(function (item) {
        return item.name === privilege
    })
})

Edit :- I hope this is what you are looking for continuing from what you started.

let val = result.roles.filter((element) => {
    return element.privileges.filter(function (item) {
        return item.name === privilege
    }).length != 0
}).length != 0

Edit 2 :- However a more efficient and understandable solution would be

function check() {
    for(role of result.roles){
        for(tmpprivilege of role.privileges){
            if(tmpprivilege.name === privilege){
                return true;
            }
        }
    }
    return false;
} 

let val = check()
console.log(val)
Sign up to request clarification or add additional context in comments.

5 Comments

Its not returning any result even though having matching data.
Check the edit, i have given it a try but your requirement is a bit unclear to me, what exactly you want. Please elaborate the scenario
I need to return from this one equal condition satisfied, either save that value in a variable or return from the method.
So this whole function, roles.forEach should return a true false based on if any element of this array contains a privilege or not.
Yes, its not returning tried with the updated answer, let val : boolean = result.roles.forEach((element: any) => { return element.privileges.filter(function (item: any) { return item.name === privilege }).length != 0 }) console.log("val "+val);
0

result.roles.forEach((element:any) => {

Use some https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

result.roles.some((element:any) => {
               return element.privileges.filter(function(item:any){
        if(item.name === privilege)
            return true;
        else 
            return false;
        }).length;

1 Comment

Its not returning any value
0

In angular you can use for:

function filterTable(result:any){
let tmpArray: any=[]
for (let result of this. result.roles){
  for(let priv of result.privileges){
     if(priv.name==="DEL"){
       tmpArray.push(result)
   }     
}
return tmpArray;}

2 Comments

This we can achieve in angular2 also, but how to return the value is my question
Now you have result in tmpArray and you can returned it.

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.