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

136
Views
Conditionally rendering an Array Method using Ternary Operator

This is a strange situation but utilizing the ternary operator to determine my array method would allow for much dryer code.

The goal:

const result = array. (isTrue ? some : every) (item) => {A lot of logic}

The above code obviously wont work but I wanted to see if there was some syntax that would allow something like this?

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

0

You can use the bracket notation to access the method conditionally:

const method = (isTrue, array) => array[isTrue ? 'some' : 'every'](item => item)

const arr = [0, 1, 2]

console.log(method(true, arr)) 
console.log(method(false, arr))

about 4 years ago · Juan Pablo Isaza Report

0

I'd just do it this way:

const myLogic = (item) => { a lot of logic };

const result = isTrue ? array.some(myLogic) : array.every(myLogic)
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!