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

252
Views
If array includes a cetain key then delete entire object using JS

I am getting back a 2d array and sometimes I will get an "error" and "message" back for that particular object. The problem I keep running into, Is that the messaging I am getting back varies...How do I go about if the "error" key is mentioned then delete the object entirely.

My Array:

let filtData = [
  [
    {
      descriptions: {
        attrs: {
          lang: "en-GB",
        },
      },
    },
    {
      descriptions: {
        attrs: {
          lang: "es",
        },
      },
    },
    {
      error: {},
      message: "Cannot read property 'attrs' of undefined"
    },
  ],
  [
    {
      descriptions: {
        attrs: {
          lang: "sp",
        },
      },
    },
    {
      descriptions: {
        attrs: {
          lang: "en-GB",
        },
      },
    },
    {
      descriptions: {
        attrs: {
          lang: "it",
        },
      },
    },
  ],
  [
    {
      descriptions: {
        attrs: {
          lang: "en",
        },
      },
    },
    {
      descriptions: {
        attrs: {
          lang: "uk",
        },
      },
    },
    {
      descriptions: {
        attrs: {
          lang: "en-GB",
        },
      },
    },
  ],
];

Js Filter - This only works when I know the messaging

let objectToBeRemove = [{ error: {}, message: "Cannot read property 'attrs' of undefined" }];
filtData = filtData.filter((obj) => objectToBeRemove.some((objToRemove) => JSON.stringify(objToRemove) !== JSON.stringify(obj)));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check that .every of the elements in the subarray lack an error property.

const input=[[{descriptions:{attrs:{lang:"en-GB"}}},{descriptions:{attrs:{lang:"es"}}},{error:{},message:"Cannot read property 'attrs' of undefined"}],[{descriptions:{attrs:{lang:"sp"}}},{descriptions:{attrs:{lang:"en-GB"}}},{descriptions:{attrs:{lang:"it"}}}],[{descriptions:{attrs:{lang:"en"}}},{descriptions:{attrs:{lang:"uk"}}},{descriptions:{attrs:{lang:"en-GB"}}}]];

const filtered = input.filter(
  subarr => subarr.every(obj => !obj.hasOwnProperty('error'))
);
console.log(filtered);

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