Created
February 8, 2022 15:03
-
-
Save sodogan/06a8d8898b9d0ee1509813316cb10552 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const characters = [ | |
{ | |
name: "Luke Skywalker", | |
height: 172, | |
mass: 77, | |
eye_color: "blue", | |
gender: "male", | |
}, | |
{ | |
name: "Darth Vader", | |
height: 202, | |
mass: 136, | |
eye_color: "yellow", | |
gender: "male", | |
}, | |
{ | |
name: "Leia Organa", | |
height: 150, | |
mass: 49, | |
eye_color: "brown", | |
gender: "female", | |
}, | |
{ | |
name: "Anakin Skywalker", | |
height: 188, | |
mass: 84, | |
eye_color: "blue", | |
gender: "male", | |
}, | |
]; | |
//Map function! | |
let _allNames = (characters) => { | |
return characters.map((character) => character.name); | |
}; | |
let _allNamesHeight = (characters) => { | |
return characters.map((character) => { | |
return { | |
name: character.name, | |
height: character.height, | |
}; | |
}); | |
}; | |
let _allFirstNames = (characters) => { | |
return characters.map((character) => { | |
let [firstName, lastName] = character.name.split(" "); | |
return firstName; | |
}); | |
}; | |
let myMapped = _allFirstNames(characters); | |
//Total Mass | |
const totalMass = characters.reduce((total, currentCharacter) => { | |
return total + currentCharacter.mass; | |
}, 0); | |
const totalHeight = characters.reduce((total, currentCharacter) => { | |
return total + currentCharacter.height; | |
}, 0); | |
const charactresByEyeColor = characters.reduce((total, currentCharacter) => { | |
let color = currentCharacter.eye_color; | |
if (total[color]) { | |
total[color] = total[color] + 1; | |
} else { | |
total[color] = 1; | |
} | |
return total; | |
}, {}); | |
console.log(charactresByEyeColor); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment