【问题标题】:Is there a way to discontinue a forEach loop once you reach a certain key:value pair in the array?一旦到达数组中的某个键:值对,有没有办法停止 forEach 循环?
【发布时间】:2020-11-04 10:10:51
【问题描述】:

总的来说,我对 Javascript 和软件开发还是很陌生,因为这是我的第一份工作,现在我正在调试一个与验证相关的严重缺陷。

我有一个场景,我有一个数组,在该数组中,每个产品都有一个标记为 Unit Volume 的属性,其代码为 ASC_Sales_Volume。

当前的缺陷是,当用户配置多个产品,但留下一个单位体积为零的产品时,尽管存在验证检查以防止这种情况发生,但仍允许用户向前导航到下一页.阻止用户前进的条件是 $scope.bpTree.response.errorCheckVolume = true

使用前开发人员当前编写的内容,当用户单击按钮以触发函数时,forEach 循环会不断将 $scope.bpTree.response.errorCheckVolume 更改回 false

Next 按钮本身是一个 OOTB 组件,我无法直接修改。

解决此问题的下一个最佳步骤是什么?基本上,当 forEach 评估 Unit Volume 值为 0 的产品时,$scope.bpTree.response.errorCheckVolume 应始终等于 true,而不管后续 Unit Volume 值如何,直到用户更改该特定产品的 Unit Volume 值,然后单击下一步按钮,逻辑再次触发。如果错误记录(如数组中单位体积为 0 的任何产品)大于零,则 errorCheckVolume 应始终评估为真,并且应阻止用户继续进行。

我会使用类似于 Array.prototype.some() 的东西吗?我一直盯着屏幕看了好几个小时,尝试不同的方法都无济于事。

这里是sn-p的代码:

function validateAllPlansUnitVolume(triggeredFromNextBtn) {
         var coveragesData = $scope.sortedCoverages[$scope.childProduct.instanceKey || $scope.childProduct.productName || $scope.childProduct.Name],
            errorRecords = checkForInvalidPlans(coveragesData, triggeredFromNextBtn);
            if(errorRecords > 0) {
               $scope.bpTree.response.errorCheckVolume = true;
               
            }
            else {
                $scope.bpTree.response.errorCheckVolume = false;
            }
     }

function checkForInvalidPlans(coveragesData, triggeredFromNextBtn){
        if(!!coveragesData && coveragesData.length) {
            coveragesData.forEach(function(product){
                if(!!product.attributeCategories){
                    unitVolumeAttrNodes = product.attributeCategories.records[0].productAttributes.records.filter((ele) => ele.code == "ASC_Sales_Volume");
                    if(!!unitVolumeAttrNodes && unitVolumeAttrNodes.length){
                        unitVolumeAttrNodes.forEach(function(attrNode){
                             if(unitVolumeAttrNodes[0].userValues === 0){
                                   $scope.bpTree.response.errorCheckVolume = true;
                                    product.unitVolumeErr = true;
                                    product.unitVolumeErrMsg = "Unit Volume cannot be zero. Please update before proceeding to the next screen"; 
                            }
                                else {
                                    product.unitVolumeErrMsg = "";
                                    product.unitVolumeErr = false;
                                    $scope.bpTree.response.errorCheckVolume = false;
                                    }
                        });
                    }
                }
            });

            return coveragesData.filter((ele) => ele.unitVolumeErr).length;
        }

        return 0;
    }

【问题讨论】:

标签: javascript angularjs loops foreach angularjs-scope


【解决方案1】:

我建议您为此目的使用Array.prototype.some()。使用 foreach 循环然后按条件中断它不是一个好习惯。 Js 对于不同的工作有不同的数组方法。我认为针对不同的要求,他们创建了构建方法,否则我们可以对所有工作使用一种方法, Visit this page select array method that fit in your requirement

【讨论】:

    【解决方案2】:

    .forEach 无法做到这一点,但 .some 可以:

    ['a', 'b', 'c'].some((element, index) => {
        console.log(element);
        return element == 'b' || index == 1;
    }); 
    

    此代码记录数组的每个元素,在到达'b' 或索引1 后停止循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 2019-01-23
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多