为什么这个函数在返回true时返回false?

时间:2021-08-25 20:37:59

I have a simple code base with less than thirty lines, it has a function called disable() where it verifies if a value of an array inside of an array inside of another array (bind to check-boxes) and return true if either:

我有一个简单的代码库,少于30行,它有一个名为disable()的函数,它验证另一个数组内的数组内部的数组值(绑定到复选框),如果有,则返回true:

  • zero or more than one checkbox checked
  • 选中零个或多个复选框

  • one checkbox checked that has a certain value of null
  • 选中一个具有特定值null的复选框

here's the function :

这是功能:

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;
}

You can find the full code here

你可以在这里找到完整的代码

1 个解决方案

#1


3  

You can not return for an outer function from an inner callback, but you could use Array#some for the nested arrays and return if true with a short circuit.

您无法从内部回调返回外部函数,但您可以将Array#some用于嵌套数组,如果为short则返回true。

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

#1


3  

You can not return for an outer function from an inner callback, but you could use Array#some for the nested arrays and return if true with a short circuit.

您无法从内部回调返回外部函数,但您可以将Array#some用于嵌套数组,如果为short则返回true。

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