Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
How can I return the value of a key based on a different value in a dictionary
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 have the above array. I have filtered the users that are in team red. Is there a way to return only the usernames of all users in team red instead of returning the whole data entry? So the output should be "[ John, Susy ]". I have this code so far.

const filterArray = array.filter(user3 => {
  return user3.team === "red";
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Since the result of filter method is an array, you can map over your filtered array and extract just username property, like this:

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 filterArray = array.filter(({team}) => team === "red").map(({username}) => username)

console.log(filterArray)

about 4 years ago · Juan Pablo Isaza Report

0

You can map the filtered array to only have the username property in it.

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 filterArray = array.filter(user3 => {
  return user3.team === "red";
})

const onlyNames = filterArray.map(u => u.username)

console.log(onlyNames)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!