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

355
Views
Why am I unable to map this array of objects when it is loaded in useState()?

learning React but I'm trying to map an array of objects in a dropdown box.

I'm getting a "Uncaught TypeError: ids.map is not a function".

Why am I getting this error when I have set the loadedIds I get from the GET request into useState's setIds?

https://jsfiddle.net/4jh9c6dv/53/

Thank you for helping a beginner :')

function Dropdown ()
{
    const [ids, setIds] = React.useState([]);

 React.useEffect(() => {
    request.get(endpointIds).then((response) => {
      setIds(response.data);
      
      const loadedIds = [];
      
      for (const id in response)
      {
        loadedIds.push({
        id: ids,
        });
      }
      
      setIds(loadedIds);
      
    });
  }, []);
  
  
 const idsList = ids.map((id) => 
 (
        <option>id</option>
 ));

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

0

You do not need to use setIds(response.data);. Remove that. response.data is not an array, response.data.ids is. That is why you get the error.

EDIT : Use for of instead of for in.

for of is used to iterate over items of an array/iterable object. for in is used to iterate over index.

about 4 years ago · Juan Pablo Isaza Report

0

response.data should be response.data.id you also forgot curly braces {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!