Skip to content

Getting the error: “data.id is undefined” while iterating a JSON array

Trying to log the data in the array but getting an error: data.id is undefined

{
  youtube.map((item, index) => {
    return console.log(youtube.id);
  });
}

This is the JSON array which is being mapped:

{
  "youtube":[
    {
      "id":"https://www.youtube.com/embed/hogbutbOgbo",
      "title":"YouTube video player"
    },
    {
      "id":"HHJkUQGm2H8",
      "title":"YouTube video player"
    },
    {
      "id":"fEE4RO-_jug",
      "title":"YouTube video player"
    },
    {
      "id":"dpqelE_9OgA",
      "title":"YouTube video player"
    },
    {
      "id":"u1NlmFa0-68",
      "title":"YouTube video player"
    },
    {
      "id":"qoUxP_h6DRs",
      "title":"YouTube video player"
    }
  ]
}

Answer

You are logging the main array instead of the mapped item, try changing to:

console.log(item.id)