I have an object like this:
const array = { "clientName": "xyz", "exp": 0, "roles": [ "dist-users", "admin-dev" ] };
I want to filter roles based on priority as admin-* then 2nd priority as dist-users, if none present then by default dist-users.How to achieve that?
filter iterates over all elements - you can't stop it.
I don't think this is the right approach, you should use Array.find or Array.findIndex to probe the roles array and figure out if it has an item starting with the desired prefix.
const array = {
"clientName": "xyz",
"exp": 0,
"prefUserName": "admin-dev",
"roles": [
"dist-users",
"admin-dev"
]
};
const admin = array.roles.find(role => role.includes('admin-'))
const dist = array.roles.find(role => role.includes('dist-users'))
const roles = [ admin || dist || 'dist-users' ]
console.log(roles)