Skip to content

Instantly share code, notes, and snippets.

@dottedsquirrel
Created August 30, 2019 01:55
Show Gist options
  • Save dottedsquirrel/f09c446cdcf5c9a23f55a71c6adb4910 to your computer and use it in GitHub Desktop.
Save dottedsquirrel/f09c446cdcf5c9a23f55a71c6adb4910 to your computer and use it in GitHub Desktop.
JavaScript array map()
let animals = [
{name: 'Tibbers', type: 'cat', isNeutered: true, age: 2},
{name: 'Fluffball', type: 'rabbit', isNeutered: false, age: 1},
{name: 'Strawhat', type: 'cat', isNeutered: true, age: 5}
]
// what you need:
// ['Tibbers', 'Fluffball', 'Strawhat']
let animalNames = animals.map(animal => {return animal.name});
// what you need:
// [{name: 'Tibbers', species: 'cat'}, {name: 'Fluffball', species: 'rabbit'}, {name: 'Strawhat', species: 'cat'}]
let petDetails = animals.map(animal => {
return {
name: animal.name,
species: animal.type
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment