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

136
Views
What is the better way to check in array of nested object

I'd like to check at least one checked field is true and not disabled:

const test = 
  [ { Title: 
      { Article: { checked: true,  disabled: true  } 
      , Desc:    { checked: false, disabled: false } 
    } } 
  , { News: 
      { Dashboard: { checked: false, disabled: false} 
    } }
  ] 

I tried like this:

const checkedItems = () => {
  let value = false;
  test.forEach(el => Object.entries(el).forEach(([title, checkboxProps]) => {
    Object.entries(checkboxProps).forEach(([name, config]) => {
      if (config["checked"] && !config["disabled"]) {
        value = true
      }
    })
  }))
  return value;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A couple of flatMaps with Object.values can do it pretty cleanly.

const test = [{
  Title: {
    Article: {
      checked: true,
      disabled: true
    },
    Desc: {
      checked: false,
      disabled: false
    }
  }
}, {
  News: {
    Dashboard: {
      checked: false,
      disabled: false
    }
  }
}];

const atLeastOneCheckedAndNotDisabled = test
  .flatMap(Object.values)
  .flatMap(Object.values) // now we have an array of [{checked, disabled}]
  .some(innerObj => innerObj.checked && !innerObj.disabled);
  
console.log(atLeastOneCheckedAndNotDisabled);

You don't care about the keys, only the values, so Object.values will make things easier to work with than Object.entries.

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!