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

205
Vistas
Cómo filtrar desde un objeto iterando sobre él en js

Estoy tratando de obtener el valor de "tipo" del objeto al iterarlo. El objeto se ve así.

 { "team": { "table": [ { "cityCode": 123, "list": { "players": [ { "name": "peter", "school": "x", "awards": { "type": "gold" }, "year": 2019 } ] } }, { "cityCode": 456, "list": { "players": [ { "name": "Dave", "school": "y", "awards": { "type": "silver" }, "year": 2018 } ] } } ] } }

Puedo obtener los valores de tipo usando esto:

 const table = team.table; for (let i = 0; i < table.length; i++) { const values = { type: table[i].list.players .filter((a) => a.awards != null) .map((a) => a.awards.type) .join(" "), }; }

Sin embargo, quiero usar otro filtro en la "lista" para filtrar listas no nulas. Entonces, ¿cómo puedo lograr eso?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Desea verificar Verificar si la clave 'list' existe dentro de un objeto JSON team.table

puedes consultar por

 if(table[i].hasOwnProperty('list')){ }

el código es

 const table = team.table; for (let i = 0; i < table.length; i++) { if(table[i].hasOwnProperty('list')){ const values = { type: table[i].list.players .filter((a) => a.awards != null) .map((a) => a.awards.type) .join(" "), }; } }
about 4 years ago · Juan Pablo Isaza Denunciar

0

1) Puede obtener todo type usando flatMap y map como:

 obj.team.table.flatMap((o) => o.list.players.map((o) => o.awards.type)) 

 const obj = { team: { table: [ { cityCode: 123, list: { players: [ { name: "peter", school: "x", awards: { type: "gold", }, year: 2019, }, ], }, }, { cityCode: 456, list: { players: [ { name: "Dave", school: "y", awards: { type: "silver", }, year: 2018, }, ], }, }, ], }, }; const types = obj.team.table.flatMap((o) => o.list.players.map((o) => o.awards.type)); console.log(types);

2) Usar forEach y desestructurar como:

 const obj = { team: { table: [ { cityCode: 123, list: { players: [ { name: "peter", school: "x", awards: { type: "gold", }, year: 2019, }, ], }, }, { cityCode: 456, list: { players: [ { name: "Dave", school: "y", awards: { type: "silver", }, year: 2018, }, ], }, }, ], }, }; const table = obj.team.table; const types = []; for (let i = 0; i < table.length; i++) { const { list: { players } } = table[i] players.forEach(({ awards: { type }}) => types.push(type)) } console.log(types);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Será más limpio usar forEach . Necesitará 2 forEach debido a su estructura de datos. Pero debajo del código:

  • verificar si los awards son nulos
  • compruebe si awards.type es nulo

 const data = { "team": { "table": [ { "cityCode": 123, "list": { "players": [ { "name": "peter", "school": "x", "awards": { "type": "gold" }, "year": 2019 } ] } }, { "cityCode": 456, "list": { "players": [ { "name": "Dave", "school": "y", "awards": { "type": "silver" }, "year": 2018 }, { "name": "Dave", "school": "y", "awards": { "type": "gold" }, "year": 2016 } ] } }, { "cityCode": 444, "list": { "players": [ { "name": "James", "school": "y", "awards": { "type": null }, "year": 2016 } ] } }, { "cityCode": 555, "list": { "players": [ { "name": "Name 101", "school": "y", "awards": { "type": "platinum" }, "year": 2016 }, { "name": "Name 102", "school": "y", "awards": { "type": null }, "year": 2016 }, { "name": "Name 103", "school": "y", "awards": null, "year": 2016 }, ] } } ] } } // Expanded your data with more items const data1 = data.team.table; let types = [] data1.forEach((item, index) => { item.list.players.forEach((player) => { const awards = player.awards; if (awards !== null && awards.type !== null) { types = [...types, awards.type]; } }) }) // Get the list of types console.log(types); // Get unique list of types let unique_types = [...new Set(types)] console.log(unique_types);

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