[Solved] filter array of strings based on priorities javascript


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)

1

solved filter array of strings based on priorities javascript