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

134
Views
Using optional chaining with array.entries

Just trying to understand optional chaining and unsure if it can be used in the following situation with .entries, that is:

for (const [index, val] of Array.from(myArray)?.entries()) {
  if (val.status === 1) {
      . . . 
       . . . 
  }
}

I basically don't want to proceed if myArray is empty.

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

0

If you look for a short null check, I think your problem is not from Array.from but it's from myArray variable. If myArray is undefined, Array.from(myArray)?.entries() will throw an error

const myArray = undefined
const entries = Array.from(myArray)?.entries() //throw an error

enter image description here

If you want to overcome this, you need to use short circuit evaluation to assign the default value [] whenever myArray is undefined or null

const myArray = undefined
const entries = Array.from(myArray || []).entries() //working!

enter image description here

If myArray is already an array (or possibly an undefined value), you can get rid of Array.from too

const myArray = undefined
const entries = myArray?.entries() || [] //assign a default value for your loop
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!