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

219
Views
How to render array of objects in javascript react if its undefined?

I want to check for the empty array in react JavaScript. But when I am using the question mark for checking that is it undefined or not then the react and is crashing.

I am using something like this:

const [verlist, setverlist] = useState([]);

In the useEffect hook I'm using setverlist(response.versions.version). Here versions is object. And in it version is an array of objects.

But when I get versions as empty object {}, my react app crashes because in console I'm getting undefined.

I'm rendering like this:

{[...verlist].reverse().map((value, id) => {})}

How do I fix this ?

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

0

You can put a check in front of your code.

verlist && verlist.length > 0 && [...verlist].reverse().map((value, id) => {})

Let me know if it works.

about 4 years ago · Juan Pablo Isaza Report

0

Change

from : [...verlist].reverse().map((value, id) => {})

to: verlist.length > 0 && [...verlist].reverse().map((value, id) => {})

about 4 years ago · Juan Pablo Isaza Report

0

If verlist is an array and can be undefined, try:

verlist && verlist.length && [...verlist].reverse().map((value, id) => {})
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!