1

I'm trying to apply a function to map each element that my promise return after a fetch request. Somehow I can log the result to the console but I cannot use it further more. Here is my code:

async function init() {
  const res = await fetch(`https://jsonplaceholder.typicode.com/users`);
  const data = await res.json();
  console.log(data);
  function getUsersNames (data){
      data.map((user, index)=>{
          console.log(user[index].name);
      })
  }
}


init();

and this is a screenshot of my output in the console: enter image description here

Any help would be great! Thanks

Emmanuel
  • 121
  • 2
  • 11

1 Answers1

2

The error is at line 7, you are calling user[index].name but it should be user.name. In the map function you are already looping through your array.

Adnan
  • 140
  • 1
  • 6