Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Array map values and return boolean

The code is as follows

const {
      aForm,
      bForm,
      eForm,
      qForm,
    } = this.form;

return (
      aForm.isEditing ||
      bForm.isEditing ||
      eForm.isEditing ||
      qForm.isEditing
    );

Is there a way to refactor this?
Something like

const forms = pick(this.form, [
      "aForm",
      "bForm",
      "eForm",
      "qForm",
    ]);
Object.values(forms).map(f => f.isEditing).join("||") //Need to return boolean value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you already have something close to what we (other viewers) would imagine. Here I’d suggest to use Array#some to achieve this. It allows you to check if one of the items matches the condition (a logical OR ||).

You have the opposite function which checks every value (so a logical AND &&).

const properties = [
  "aForm",
  "bForm",
  "eForm",
  "qForm",
]

return properties.some(prop => {
  const obj = this.form[prop] // Get your form item

  return obj.isEditing;
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading