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

107
Views
Loop through array of objects and check conditions

I have an array of object like this

list = [
        {
          "label" : "whatever",
          "value" : "value 1"
        },
        {
          "label" : "whatever",
          "value" : "value 2"
        },
        {
          "label" : "Required scenario only",
          "value" : "value 3"
        },
        {
          "label" : "whatever",
          "value" : "value 4"
        },       
       ]

I am running for a loop with conditions, I want to execute some piece of code only for one scenario called 'Required scenario only' for the rest of all scenarios run different code for only one time, it should not execute 3 times as per loop executes

for(i=0; i< list.length; i++) {
 if(list[i].label === 'Required scenario only' ) {
    //execute some code
  } else {
    // execute code for 1 time for rest  of the labels i.e for all 'whatever' scenarios
  } 
}

How should I do it?

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

0

If you're merely checking for the existence of an object with label = "Required scenario only" then I would use Array.some():

list = [{
    label: "whatever",
    value: "value 1"
  },
  {
    label: "whatever",
    value: "value 2"
  },
  {
    label: "Required scenario only",
    value: "value 3"
  },
  {
    label: "whatever",
    value: "value 2"
  },
]
if (list.some(l => (l.label == "Required scenario only"))) {doSomething();}
else {doSomethingElse();}

function doSomething() {console.log("Do Something");}
function doSomethingElse() {console.log("Do Something Else");}

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!