Skip to content

Instantly share code, notes, and snippets.

@sodogan
Created February 8, 2022 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sodogan/06a8d8898b9d0ee1509813316cb10552 to your computer and use it in GitHub Desktop.
Save sodogan/06a8d8898b9d0ee1509813316cb10552 to your computer and use it in GitHub Desktop.
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