Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
filter array of strings based on priorities javascript

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!