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

157
Visualizações
How would I go about filtering this JavaScript object efficiently?

I have the following data:

let types = ["A", "C"];

let data = {
  301: {
    name: "Suite",
    fields: [
      [null, null, null, null, null, null],
      [
        null,
        null,
        {
          type: "A",
        },
        {
          type: "A",
        },
        null,
        null,
      ],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
    ],
  },
  302: {
    name: "Suite",
    fields: [
      [null, null, null, null, null, null],
      [
        {
          type: "B",
        },
        {
          type: "B",
        },
        {
          type: "B",
        },
        {
          type: "B",
        },
        null,
        null,
      ],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
      [null, null, null, null, null, null],
    ],
  },
};

in the fields array there is either null or an object, if there is an object I want to check wether that object has a type that is included in types, if it isn't I want to set that array value to null aswell, otherwise just skip it.

How would I do this? Any help appreciated.

I tried this approach, which works but is really terrible:

Object.keys(data).forEach((key) => {
  data[key].fields.forEach((fieldArray, fieldArrayIndex) => {
    fieldArray.forEach((field, fieldIndex) => {
      if (field) {
        if (!types.includes(field.type)) {
          fieldArray[fieldIndex] = null;
        }
      }
    });
  });
});

about 4 years ago · Juan Pablo Isaza
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