0

An array is given below. I would like to get the username for every single member of this array whose team is red!

const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];

I've tried this code:

const colorTeam = array.filter(teams=>teams.team === 'red');
console.log('teamColor:', username);

It didn't work!

1
  • When you do console.log('teamColor:', username);, where is username supposed to be coming from? And why is that line not using colorTeam from the previous line? Commented Dec 14, 2020 at 11:51

4 Answers 4

2

As opposed to filter()+map() (two passes over the source array), one may use Array.prototype.reduce() to achieve that in a single pass (which may give certain performance gain should input array be large enough, or such filtering performed often enough):

const array = [{username:"john",team:"red",score:5,items:["ball","book","pen"]},{username:"becky",team:"blue",score:10,items:["tape","backpack","pen"]},{username:"susy",team:"red",score:55,items:["ball","eraser","pen"]},{username:"tyson",team:"green",score:1,items:["book","pen"]}],

      redTeamNames = array.reduce((acc, {username, team}) => 
        (team == 'red' && acc.push(username), acc), [])
        
console.log(redTeamNames)
.as-console-wrapper{min-height:100%;}

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

Comments

0

If this is what you wanted? First we filter objects with team === red, then we map the array to contain username property only.

const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];

const colorTeam = array
  .filter(teams=>teams.team === 'red')
  .map(user=>user.username);
console.log('teamColor:', colorTeam);

1 Comment

I previously used just those two lines of code:const colorTeam = array .filter(teams=>teams.team === 'red'). It shows all features of an array's members that contain team of red color! The line of code .map(user=>user.username); is key line of code! So I grabbed the logic! Thank you!
0

user Array.filter followed by Array.map:

const array=[{username:"john",team:"red",score:5,items:["ball","book","pen"]},{username:"becky",team:"blue",score:10,items:["tape","backpack","pen"]},{username:"susy",team:"red",score:55,items:["ball","eraser","pen"]},{username:"tyson",team:"green",score:1,items:["book","pen"]}];

let result = array.filter(({team}) => team === "red").map(({username}) => username)

console.log(result)

Comments

0

In one line:

let newArray = array.filter((el) => el.team==="red" ? el : "").map((el) => el.username);

First, filter objects with team property named "red", then use map method to get username properties only.

2 Comments

Using .filter() this way is a bit overkill: .filter() returns all items of the source array for which callback passed as an argument evaluates to truthy value, so simple array.filter((el) => el.team==="red") (where boolean true/false is returned) is absolutely enough, and there's no need to overburden that with ternary, returning object (evaluated truthy) or empty string (evaluated falsy).
And, of course, your approach is nothing different to 2 answers posted earlier, so it is not clear which extra value you attempted to bring in, so more comprehensive explanation would make sense.

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.