如何确保对象键中的选择器匹配的所有元素都具有非空值?主要只是试图避免 if 语句中出现太多参数。我试过:var credit_map = { "#first": "First Name", "#middle": "Middle Name", "#last": "Last Name", "#email": "Email Address", "#postalcode": "Zip/Postal Code", "#card_name": "Name on Card", "#card_number": "Card Number", "#card_month": "Card Experation Month", "#card_year": "Card Experation Year", "#card_verification": "Card Verification Code"}for (var [key, value] of Object.entries(credit_map)) { if ($(key).val() != null || undefined) { }}
1 回答
互换的青春
TA贡献1797条经验 获得超6个赞
我想你是在问如何确保对象键中的选择器匹配的所有元素都具有非空白值。如果是这样,您可以使用以下各项
:
if (Object.keys(credit_map).every(sel => $(sel).val())) {
// Yes, they all have non-blank values
} else {
// No, at least one of them has a blank value (or didn't exist at all)
}
这之所以有效,是因为 和 都是虚假的值,并且所有非空白字符串都是真实的。 将返回值转换为布尔值并将其用作标志。""undefinedevery
添加回答
举报
0/150
提交
取消