Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I write this map function in the pre ES6 format?

Hi I have this map function I found in a tutorial, and I’m interested as to how I’d write this in the old way – i.e writing the word "function" rather than the "=>" arrow format.

const example = spaceships.map((spaceship) => ({
    homePlanet: spaceship.homePlanet,
    color: spaceship.color
}));

I assumed that I could write it like this, but I get an error when adding the extra brackets.

const example = spaceships.map(function(spaceship) ({
    homePlanet: spaceship.homePlanet,
    color: spaceship.color
}));

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to add a return

const example = spaceships.map(function (spaceship) {
  return {
    homePlanet: spaceship.homePlanet,
    color: spaceship.color
  }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading