Within my object array, I am currently filtering against the property name "username" using the following code snippet:
array = [{
"id": 1,
"username": "admin",
"roles": [{
"name": "Administrator"
},
{
"name": "agent"
}
]
},
{
"id": 2,
"username": "admin2",
"roles": [{
"name": "Administrator2"
},
{
"name": "agent2"
}
]
},
{
"id": 3,
"username": "admin3",
"roles": [{
"name": "Administrator3"
},
{
"name": "agent3"
}
]
}
]
The filtering function looks like this:
transform(array: any, valueToSearch: string): any[] {
return array.filter(e =>
e.username.toLowerCase().indexOf(valueToSearch.toLowerCase())
!== -1);
}
While this setup is functioning correctly, I now need to filter against the property name "name" within the "roles" array of each object. For example, I want to retrieve an object where the "roles" array contains a role with the name "agent3". This should return the last object in my example. I attempted the following approach:
return agents.filter(e => e.roles.filter(ee =>
ee.valueToSearch.toLowerCase()) !== -1));
However, this method did not yield the desired results.
Here is a demonstration of the issue: https://stackblitz.com/edit/angular-txchxs?embed=1&file=src/app/agentFilter.pipe.ts