映射数组的值并返回布尔值

huangapple go评论59阅读模式
英文:

Array map values and return boolean

问题

The code can be refactored like this:

const forms = pick(this.form, [
  "aForm",
  "bForm",
  "eForm",
  "qForm",
]);
const isEditingArray = Object.values(forms).map(f => f.isEditing);
return isEditingArray.includes(true);
英文:

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.

答案1

得分: 5

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;
})
英文:

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

答案2

得分: -1

const forms = [
"aForm",
"bForm",
"eForm",
"qForm",
];
const o = Object.values(forms).map(f => eval(f).isEditing).join("||")
eval(o)

英文:
const forms = [
  "aForm",
  "bForm",
  "eForm",
  "qForm",
];
const o = Object.values(forms).map(f => eval(f).isEditing).join("||")
eval(o)

huangapple
  • 本文由 发表于 2023年6月12日 20:24:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定