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

96
Views
Filter object key by on comparing values under different keys

I would like to filter results such that when I compare the value under Score with the value under against, I return the winning team

Using this as an example

const newData = [
    {"Year": 1,
    "Score": 3,
    "Against": 5,
    "GameID": 2,
    "Team": 'A' },
    {"Year": 1,
    "Score": 5,
    "Against": 3,
    "GameID": 2,
    "Team": 'B' }
]

My desired output would be ['B']

This is what I have tried

const newArr = newData.filter(element => element.Score > element.Against)
console.log(newArr.Team)

This returns undefined

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

newArr is an array, and the property Team does not relate to it, but to its elements. You can use map to convert the array of objects to an array of team name(s):

const newData = [{"Year": 1,"Score": 3,"Against": 5,"GameID": 2,"Team": 'A'},{"Year": 1,"Score": 5,"Against": 3,"GameID": 2,"Team": 'B'}]

const newArr = newData.filter(element => element.Score > element.Against)
                      .map(element => element.Team);
console.log(newArr)

about 4 years ago · Juan Pablo Isaza Report

0

You are getting this as resault:

enter image description here

You can do it by accessing newArr[0].Team

const newData = [
    {"Year": 1,
    "Score": 3,
    "Against": 5,
    "GameID": 2,
    "Team": 'A' },
    {"Year": 1,
    "Score": 5,
    "Against": 3,
    "GameID": 2,
    "Team": 'B' }
]
const newArr = newData.filter(element => element.Score > element.Against)
console.log(newArr[0].Team)

And it will print B

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!