Javascript 想用Lodash缩短我的if语句吗

Javascript 想用Lodash缩短我的if语句吗,javascript,lodash,Javascript,Lodash,我希望使用Lodash优化下面的代码。我在网上寻找解决方案,但找不到任何相关的。有人能帮忙吗 if (isPage && isPageType === constants.keys.isDisclosureSubmitted) { if (form && form.application && typeof form.application.disclosureAccepted !== 'undefined' && form

我希望使用Lodash优化下面的代码。我在网上寻找解决方案,但找不到任何相关的。有人能帮忙吗

if (isPage && isPageType === constants.keys.isDisclosureSubmitted) {
    if (form && form.application && typeof form.application.disclosureAccepted !== 'undefined' && form.application.disclosureAccepted !== null && !form.application.disclosureAccepted) {
        return $q.when(response.data.content);
    }
}
基本上,使用u.get,您不必担心检查undefined的属性,因为在该场景中它只会返回undefined,而不会抛出错误。isNil检查值是否为null或未定义,然后要确保该值仍然为false


我的意思是,我不确定lodash在这种情况下是否“优化”,因为这些函数调用实际上会降低它的速度,但它的可读性稍微好一点。就像这些评论一样,我不确定你为什么需要如此具体;你可以只使用u.get(form,['application','displationaccepted'])==false,我认为我反对使用非ICT等价性。

你的
null/undefined
检查比必要的要详细得多。您只需使用
!=null
来处理这两个问题,并且您从来都不需要
类型的
测试。您是否有理由不接受
null
未定义的
作为
false
评估,但您会接受
NaN
0
?因为如果您真的想知道它是
false
,您可以通过
==false
,绕过
null/undefined
检查来进一步缩短它。
if (isPage && isPageType === constants.keys.isDisclosureSubmitted) {
  const discAcc = _.get(form, ['application', 'disclosureAccepted'], true);
  if (!_.isNil(discAcc) && !disAcc) {
     return $q.when(response.data.content);
  }
}