簡體   English   中英

為什么這個函數在返回true時返回false?

[英]why is this function returning false when it should return true?

我有一個簡單的代碼庫,少於30行,它有一個名為disable()的函數,它驗證另一個數組內的數組內部的數組值(綁定到復選框)並返回true,如果:

  • 選中零個或多個復選框
  • 一個復選框選中具有的一定值null

這是功能:

disable() {
  if (!this.checked_y.length) {
    return true;
  }
  this.checked_y.forEach(year => {
    year.specs.forEach(sp => {
      if (sp.spec == null) {
        return true;

      }
    });
  });
  return false;
}

你可以在這里找到完整的代碼

您無法從內部回調返回外部函數,但是您可以將Array#some用於嵌套數組,如果為short則返回true。

function disable() {
    return !this.checked_y.length
        || this.checked_y.some(year => year.specs.some(sp => sp.spec == null));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM