Given an object that looks like this:
let abilities = {
"technical": {
"Corners": 12,
"Crossing": 12,
"Dribbling": 20,
"Finishing": 14,
"First Touch": 17,
"Free Kick": 13,
"Heading": 7,
"Long Shots": 11,
"Long Throws": 5,
"Marking": 3,
"Passing": 15,
"Penalty Taking": 19,
"Tackling": 4,
"Technique": 18
},
"mental": {
"Aggression": 8,
"Anticipation": 12,
"Bravery": 17,
"Composure": 15,
"Concentration": 13,
"Decisions": 16,
"Determination": 15,
"Flair": 18,
"Leadership": 6,
"Off The Ball": 14,
"Positioning": 7,
"Teamwork": 9,
"Vision": 16,
"Work Rate": 12
},
"physical": {
"Acceleration": 17,
"Agility": 20,
"Balance": 16,
"Jumping Reach": 8,
"Natural Fitness": 16,
"Pace": 16,
"Stamina": 17,
"Strength": 11
}
}
I want to get the keys and values of the 5 highest and 5 lowest values.
I first tried to get the top integer value in each object inside the abilities object by doing:
Object.keys(abilities).forEach(key => {
let value = abilities[key];
console.log(key)
console.log(value)
let maxval = Object.keys(abilities).reduce((a, b) => abilities[a] > abilities[b] ? a : b);
console.log(maxval)
});
This prints out the name of the inner objects and the entire sub-object itself.
> technical
> {Corners: 12, Crossing: 12, Dribbling: 20, Finishing: 14, First Touch: 17, ...}
However, the maxval doesn't give anything related to the Max.
How may I solve my task?
technicalandmental)?top = {"Dribbling": 20, "Agility": 20, "Penalty Taking": 19, "Technique": 18, "Flair": 18}and 'low = {"Marking": 3, "Tackling": 4, "Long Throws": 5, "Leadership": 6, "Heading": 7}` Separating into two functions is also fine. It's the top 5 and lowest 5 abilities among all objects.