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

88
Views
How to specify multiple conditions in an array instead of using multiple " | | "

I have an array of conditions

let conditions = [1,3,5,7,9];
let values = [1,2,3,4,5,6,7,8,9,0]

I want

if (values == 1 || values== 3 || values == 5 || values == 7 || values == 9 ){   
  return values * 3  
} else { 
  return value * 2 
}

can someone please help to avoid writing multiple OR Conditions and get this done within 1 statement? Thank you

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

0

First of all: you can iterate throught yours values using map operator and then check via includes

let conditions = [1,3,5,7,9];
let values = [1,2,3,4,5,6,7,8,9,0]

const result = values.map(val => conditions.includes(val) ? val * 3 : val * 2);

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

I'd suggest creating a Set from your conditions, this will be more efficient for large datasets. You can then use Array.map() to create your result:

let conditions = new Set([1,3,5,7,9]);
let values = [1,2,3,4,5,6,7,8,9,0]

function getResult(values, conditions) {
   return values.map(value => conditions.has(value) ? value * 3: value * 2)
}

console.log('getResult:', getResult(values, conditions));

about 4 years ago · Juan Pablo Isaza Report

0

you can do this:

const toAvoidArray = [1, 3, 5, 7, 9]
if (toAvoidArray.indexOf(value) !== -1) {
  return values * 3
} else {
  return value * 2
}

or in your case,you can do this:

if (value%2!==0) {
  return values * 3
} else {
  return value * 2
}
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!