簡體   English   中英

當我的循環內條件滿足時,如何僅顯示特定的控制台

[英]How do i display only the specific console when my condition inside loop is met

這是我的控制器功能:

$scope.Save = function () {

    for(var i = 0; i < $scope.listOfnames.length; i++){
         if($scope.listOfnames[i].surname == $scope.name.surname){
            console.log("This surname is already exist!");

        }else{
            console.log("save this surname.");
            }
    }
}

這是我在控制台中的輸出:

save this surname.

This surname is already exist!

save this surname.

例如,我的 $scope.listOfnames 中已經有一個“STUART”姓氏,當我在我的姓氏輸入字段中輸入“STUART”姓氏時,它必須顯示“這個姓氏已經存在!” 僅在我的控制台中。

我知道我的 for 循環有問題,我應該怎么做?

您在控制台上得到它,因為“STUART”是陣列上的第二個值。 您可以執行以下操作:

$scope.Save = function () {
    var found  = false;

    //Loop thru the array and check each. If found change the value of var found
    for(var i = 0; i < $scope.listOfnames.length; i++){
        if( $scope.listOfnames[i].surname == $scope.name.surname ){
              found = true;
        }
    }


    if ( found ) {
        console.log("This surname is already exist!");
    } else {
        console.log("save this surname.");
    }
}

暫無
暫無

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

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