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

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