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?
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(" "), }; } }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);Será más limpio usar forEach . Necesitará 2 forEach debido a su estructura de datos. Pero debajo del código:
awards son nulosawards.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);