Function Called inside forEach returns undefined

Just curious. I’m working with an array of objects and I want to destructure some properties from it. However. A working function is returning undefined when called from within a forEach loop.

I know that forEach returns undefined, but I wanted input as to why I wasn’t able to access this data from within a forEach.

Working example

const arr = [
  {
    id: 4952,
    url:
      "http://www.tvmaze.com/episodes/4952/game-of-thrones-1x01-winter-is-coming",
    name: "Winter is Coming",
    season: 1,
    number: 1,
    airdate: "2011-04-17",
    airtime: "21:00",
    airstamp: "2011-04-18T01:00:00+00:00",
    runtime: 60,
    image: {
      medium:
        "http://static.tvmaze.com/uploads/images/medium_landscape/1/2668.jpg",
      original:
        "http://static.tvmaze.com/uploads/images/original_untouched/1/2668.jpg",
    },
    summary:
      "<p>Lord Eddard Stark, ruler of the North, is summoned to court by his old friend, King Robert Baratheon, to serve as the King's Hand. Eddard reluctantly agrees after learning of a possible threat to the King's life. Eddard's bastard son Jon Snow must make a painful decision about his own future, while in the distant east Viserys Targaryen plots to reclaim his father's throne, usurped by Robert, by selling his sister in marriage.</p>",
    _links: {
      self: {
        href: "http://api.tvmaze.com/episodes/4952",
      },
    },
  },
  {
    id: 4953,
    url:
      "http://www.tvmaze.com/episodes/4953/game-of-thrones-1x02-the-kingsroad",
    name: "The Kingsroad",
    season: 1,
    number: 2,
    airdate: "2011-04-24",
    airtime: "21:00",
    airstamp: "2011-04-25T01:00:00+00:00",
    runtime: 60,
    image: {
      medium:
        "http://static.tvmaze.com/uploads/images/medium_landscape/1/2669.jpg",
      original:
        "http://static.tvmaze.com/uploads/images/original_untouched/1/2669.jpg",
    },
    summary:
      "<p>An incident on the Kingsroad threatens Eddard and Robert's friendship. Jon and Tyrion travel to the Wall, where they discover that the reality of the Night's Watch may not match the heroic image of it.</p>",
    _links: {
      self: {
        href: "http://api.tvmaze.com/episodes/4953",
      },
    },
  },
  {
    id: 4954,
    url: "http://www.tvmaze.com/episodes/4954/game-of-thrones-1x03-lord-snow",
    name: "Lord Snow",
    season: 1,
    number: 3,
    airdate: "2011-05-01",
    airtime: "21:00",
    airstamp: "2011-05-02T01:00:00+00:00",
    runtime: 60,
    image: {
      medium:
        "http://static.tvmaze.com/uploads/images/medium_landscape/1/2671.jpg",
      original:
        "http://static.tvmaze.com/uploads/images/original_untouched/1/2671.jpg",
    },
    summary:
      "<p>Jon Snow attempts to find his place amongst the Night's Watch. Eddard and his daughters arrive at King's Landing.</p>",
    _links: {
      self: {
        href: "http://api.tvmaze.com/episodes/4954",
      },
    },
  },
  {
    id: 4955,
    url:
      "http://www.tvmaze.com/episodes/4955/game-of-thrones-1x04-cripples-bastards-and-broken-things",
    name: "Cripples, Bastards, and Broken Things",
    season: 1,
    number: 4,
    airdate: "2011-05-08",
    airtime: "21:00",
    airstamp: "2011-05-09T01:00:00+00:00",
    runtime: 60,
    image: {
      medium:
        "http://static.tvmaze.com/uploads/images/medium_landscape/1/2673.jpg",
      original:
        "http://static.tvmaze.com/uploads/images/original_untouched/1/2673.jpg",
    },
    summary:
      "<p>Tyrion stops at Winterfell on his way home and gets a frosty reception from Robb Stark. Eddard's investigation into the death of his predecessor gets underway.</p>",
    _links: {
      self: {
        href: "http://api.tvmaze.com/episodes/4955",
      },
    },
  },
];

arr.forEach(obj => {
  console.log(obj);
  const { currentEpisode } = cleanedEpisodeData(obj)
  console.log(currentEpisode);
})

function cleanedEpisodeData(current) {
  const currentEpisode = {
    name: current.name,
    season: ("0" + current.season).slice(-2),
    number: ("0" + current.number).slice(-2),
    imageSrc: current.image.medium,
    summary: current.summary.replace(/<\w+>|<\/\w+>/g,''),
  }
  return currentEpisode;
}

I know that this problem occurs both in and outside of the browser so I’ve ruled out extension interference. Will go over the docs, but does anyone know the reasoning/any work around for this kind of programming technique? It’s a bit annoying it doesn’t work how I expect it to, but there may be a really good reason for it.

I’m not sure I understand why you’re trying to use destructuring here?
If you remove the curly brackets, does your code not do exactly what you want?

1 Like

Just a brain fart. Thanks pal :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.