Ivan Vrzogic
Ivan Vrzogic

Reputation: 157

From array of objects extract array of property values for items having another property equal to given value

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!

Upvotes: 0

Views: 62

Answers (4)

Yevhen Horbunkov
Yevhen Horbunkov

Reputation: 15530

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%;}

Upvotes: 2

waluga
waluga

Reputation: 170

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.

Upvotes: 0

Alan Omar
Alan Omar

Reputation: 4217

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)

Upvotes: 0

Alexey Victorov
Alexey Victorov

Reputation: 433

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

Upvotes: 0

Related Questions