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
}));
>Solution :
You have to add a return
const example = spaceships.map(function (spaceship) {
return {
homePlanet: spaceship.homePlanet,
color: spaceship.color
}
});