0

    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?

2
  • Excuse me, could you precise your desired result? You want to get e.g. Bob instead of Bob Jones? Commented Jul 30, 2017 at 16:11
  • and if so, should they be unique? Commented Jul 30, 2017 at 16:13

3 Answers 3

4

You could use Array#filter to firstly get every players age that fulfills the condition and then Array#map to get just their names using string split.

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 res = team.filter(v => v.age > 19 && v.age < 26)
              .map(v => v.name.split(" ")[0]);
              
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use map and for each name, split on white space and return the first element:

age(team).map(fullname => fullname.split(" ")[0])

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));

console.log(
  age(team).map(fullname => fullname.split(" ")[0])
)

Comments

0

You could get first all objects and then map only the first name of the objects as result set.

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}],
    age = team => team.filter(members => members.age >= 20 && members.age <= 25)
                      .map(members => members.name.split(' ')[0]);

console.log(age(team));//[ 'Bob Jones', 'John Anderson', 'Tim Hamburger' ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.