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

117
Views
How can I call another element from an array with the same name?

I'm relatively new to JavaScript / React.js. I have a random API from which I want to read an item (in this example an email adress).

What if the array has more emails for different users?

In that case, how can I call that specific email, let's say 2nd and 5th. What is the syntax for that?

Code is given bellow:

   import {useState,useEffect} from 'react';

function App(){

  const url = 'https://randomuser.me/api/';

  const [information,setInformation]=useState([]);
  
  async function getData(){
    const resp = await fetch(url);
    const data = await resp.json();
    console.log(data);
    const [item]=data.results;
    setInformation(item);
  }

  useEffect(()=>{
    getData();
  },[])

  return(
    <>
      {information.email}
    </>
  )
}
export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since you are getting the data as an array, of the format

email: ["a@gmail.com", "b@gmail.com"]

You can map them as :

export default function App() {
  let information = { email: ["a@gmail.com", "b@gmail.com"] };
  return (
    <ul>
      {information.email.map((item, i) => (
        <li key={i}>{item}</li>
      ))}
    </ul>
  );
}

Or to display a particular email, say of 1st location, access them as:

   <>
      {information.email[1]}
    </>
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!