map CODE
The
map
function calls
getNames
function for each item of the array. Each time the function
getNames
returns only names from each object
let myFavoritePlanets = [
    {name: 'Earth', radius: 6371}, 
    {name: 'Jupiter', radius: 69911}, 
    {name: 'Neptune', radius: 24622, moons: ['Triton', 'Thalassa', 'Nereid', 'Proteus', 'Hippocamp', 'Naiad']}
]
let namesOnly = myFavoritePlanets.map(getNames);

function getNames(planet) {
  return planet.name
}
console.log(namesOnly);
by Valeri Tandilashvili
4 years ago
JavaScript
Array functions
2
Pro tip: use ```triple backticks around text``` to write in code fences