let team = [
{'name' : 'Bob Jones', 'Info': {'Height': '6.4', 'Weight': 170}, 'age': 20},
{'name' : 'John Anderson', 'Info': {'Height': '5.4', 'Weight': 120}, 'age': 23},
{'name' : 'Tim Hamburger', 'Info': {'Height': '6.1', 'Weight': 180}, 'age': 25},
{'name' : 'Tom Hamburger', 'Info': {'Height': '6.6', 'Weight': 220}, 'age': 30},
{'name' : 'Jack Johnson', 'Info': {'Height': '6.0', 'Weight': 190}, 'age': 41},
{'name' : 'Tommy Tubbs', 'Info': {'Height': '6.1', 'Weight': 180}, 'age': 50},
]
let age = (team) => {
let earlyTwenties = [];
team.filter((members) => {
if(members.age >= 20 && members.age <= 25) {
earlyTwenties.push(members.name)
}
});
return earlyTwenties;
}
console.log(age(team));//[ 'Bob Jones', 'John Anderson', 'Tim Hamburger' ]
I have a function that filters all the team members that are in their early twenties. I need to return only the team members first names instead of their full names.
I know I can use a for-loop and split each name and then use another for loop to return every other value giving me all the first names, but I'm trying to write better code using HOF.
Any suggestions on how to use map to split the array and return only the first name all in the same function?
Bobinstead ofBob Jones?