英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论