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

100
Views
Javascript iterate an array with two conditions

I have this array and these values:

const levels = [
    {
       name: 'first level primary school'     
    }, 
    {
       name: 'second level primary school'     
    },
    {
      name: 'first level secondary school'     
    }
]

const isPrimarySchool = false
const isSecondarySchool = false

And I want to iterate the array and find if there is a string with the word 'primary' or 'secondary' and turn the value of isPrimarySchool or isSecondarySchool to true.

The method that I found to resolve this, is this one:

levels.forEach(level => {
            if (
              level.name.toLowerCase().includes('primary')
            ) {
              isPrimarySchool = true
            }
            if (
              level.name.toLowerCase().includes('secondary')
            ) {
              this.isSecondarySchool = true
            }
          })

Is there a better way to resolve this, using lodash or normal javascript?

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

0

You could take a function for using with Array#some and hand over the expected value.

const
    has = string => o => o.name.includes(string),
    levels = [{ name: 'first level primary school' }, { name: 'second level primary school' }, { name: 'first level secondary school' }],
    isPrimarySchool = levels.some(has('primary')),
    isSecondarySchool = levels.some(has('second'))
    
console.log(isPrimarySchool);
console.log(isSecondarySchool);

about 4 years ago · Juan Pablo Isaza Report

0

I want to iterate the array and find if there is a string with the word 'primary' or 'secondary' and turn the value of isPrimarySchool or isSecondarySchool to true.

Using some() with includes

const levels = [
    {
       name: 'first level primary school'     
    }, 
    {
       name: 'second level primary school'     
    },
    {
      name: 'first level secondary school'     
    }
]
const isPrimarySchool = levels.some(x=>x.name.toLowerCase().includes('primary'))
const isSecondarySchool = levels.some(x=>x.name.toLowerCase().includes('secondary'))
console.log({isPrimarySchool,isSecondarySchool})

about 4 years ago · Juan Pablo Isaza Report

0

You could use the built-in Array.some() function and some regexp:

isPrimarySchool = levels.some(level => /primary/.exec(level.name));
isSecondarySchool = levels.some(level => /secondary/.exec(level.name));
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!