1

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;
};

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer +1. Would love to connect with you via LinkedIn: linkedin.com/in/tim-biegeleisen-557aa38?originalSubdomain=sg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.