【发布时间】:2021-07-12 05:01:26
【问题描述】:
我正在尝试查看一个对象和一些属性,我可以将它们分配给的值数量有限。以下是我目前检查它们的价值的方法:
for (const [attribute, value] of Object.entries(params.object)) {
if(attribute == "accountType")
if(value != "external" && value != "internal")
return { status: `Failed! '${attribute}' must only equal to 'internal' or 'external'` };
else if(attribute == "dnsName")
if(value.match('^[a-z]*-[0-9]*$') == null)
return { status: `Failed! '${attribute}' must follow the pattern [a-z]*-[0-9]*` };
else if(attribute == "edition")
if(value != "starter" && value != "basic" && value != "classic")
return { status: `Failed! '${attribute}' must only equal to 'starter', 'basic' or 'classic'` };
else if(attribute == "forceProvision" || attribute == "installSoltions")
if (typeof value !== "boolean")
return { status: `Failed! '${attribute}' must be of type boolean true/false` };
}
有没有办法优化这个?
【问题讨论】:
-
为了每个人的理智,即使是单语句
if正文也要使用大括号,尤其是在使用多个if和else if时! Javascript 不关心您的缩进级别...
标签: javascript node.js object filter lodash