Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
How to filter from an object by iterating over it in js

I am trying to get the value of "type" from the object by iterating over it. The object looks like this.

{
  "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
            }
          ]
        }
      }
    ]
  }
}

I am able to get the type values using this:

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(" "),
  };
}

However, I want to use another filter on the "list" to filter non null lists. So how can I achieve that.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You want to check Check if 'list' key exists inside a team.table JSON object

you can check by

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

}

code is

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 Relatório

0

1) You can get all type using flatMap and map as:

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) Using forEach and destructuring as:

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 Relatório

0

It will be cleaner to use forEach. You will need 2 forEach due to your data structure. But below code will:

  • check if awards is null
  • check if awards.type is null

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda