Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda