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

156
Vistas
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 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