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

207
Views
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 answers
Answer question

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 Report

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 Report

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 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!