skip to Main Content

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.

2

Answers


  1. 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;
    })
    
    Login or Signup to reply.
  2. const forms = [
      "aForm",
      "bForm",
      "eForm",
      "qForm",
    ];
    const o = Object.values(forms).map(f => eval(f).isEditing).join("||")
    eval(o)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search