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

126
Views
Filtering Javascript array of objects by account type

I have a javascript array that contains objects which have a few properties of which there are some booleans. I want to take the user's account type and filter out the ones that don't apply to them. I've included the filter code I have tried however it isn't working because it is filtering out if only one of the if statements come true. I understand this but am unsure how to resolve it.

Filter code

if(acc_type != 'Admin') {
      this.items = this.items.filter((item) => {
        return item.admin != true
      })
    }
    if(acc_type != 'Manager') {
      this.items = this.items.filter((item) => {
        return item.manager != true
      })
    }

Some objects from array this filter are acting on

{ header: "Management", admin: true, manage: true },
    {
        title: 'Creation Form',
        to: '/management/creation-form',
        admin: true,
        manage: true,
    },
    {
        title: 'Management',
        to: '/management',
        admin: true,
        manage: true,
    },
    { header: "Settings" },
    {
        title: 'Account',
        to: '/settings/account',
        admin: true
    },
    {
        title: 'Billing',
        to: '/billing',
        admin: true
    },
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

From how I understand:

this.items = this.items.filter(function(item){
  if(acc_type == 'Admin')
    return item.admin || !item.manager
  else if(acc_type == 'Manager')
    return item.manager || !item.admin
  else // any other acct
    return !item.admin && !item.manager
})
about 4 years ago · Juan Pablo Isaza Report

0

  this.items = this.items.filter((item) => {
    return acc_type != 'Admin' && item.admin != true && 
           acc_type != 'Manager' && item.manager != true
  })
about 4 years ago · Juan Pablo Isaza Report

0

Simply turn around your logic to make it work.

If it is an 'Admin' account, every item having admin: true should be in the resulting list. If the account is is 'Manager', everything having manage: true should be in the resulting list.

if (acc_type === 'Admin') {
  this.items = this.items.filter(item => !!item.admin);
}
else if (acc_type === 'Manager') {
  this.items = this.items.filter(item => !!item.manager);
}

According to this, for 'Admin' accounts all entries will be in the resulting list, for 'Manager' accounts there will be only the first two entries.

Don't be confused by the !! - it's only a boolean conversion. For this example it can be omitted.

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!