【问题标题】:Conditional statement to check if an array is empty with angular JS使用角度JS检查数组是否为空的条件语句
【发布时间】:2015-07-01 22:35:56
【问题描述】:

我有这个功能:

$scope.doPaste = function(destination) {                            
   if ($scope.selectCopy.ids != []) {
       console.log("will copy");
       $scope.CopyFiles(destination);
   }
   if ($scope.selectMove.ids != []) {
       console.log("will move");
       $scope.MoveFiles(destination);
   }                                
};

在我的应用中,$scope.selectMove.ids$scope.selectCopy.ids 不能都为非空。我的意思是,例如当$scope.selectMove.ids 为非空时$scope.selectCopy.ids 为空。

我的问题是在控制台中,我总是看到两者都会复制和移动。

【问题讨论】:

  • 你最好检查.length属性!

标签: javascript arrays angularjs


【解决方案1】:

我认为你应该通过angular.isObject() 进行检查,如果它是一个对象,它将返回 true。

$scope.doPaste = function(destination) {
   if (angular.isObject($scope.selectCopy.ids) && $scope.selectCopy.ids.length > 0) {
       console.log("will copy");
       $scope.CopyFiles(destination);
   }

   if (angular.isObject($scope.selectMove.ids) && $scope.selectMove.ids.length > 0){
       console.log("will move");
       $scope.MoveFiles(destination);
   }                               
};

【讨论】:

    【解决方案2】:

    可能你需要使用if else条件:

    if (empty){
      console.log('empty');
    }else{
      console.log('not empty');
    }
    

    在您的代码中。是这样的:

    $scope.doPaste=function(destination) {
       if ($scope.selectCopy.ids && $scope.selectCopy.ids.length > 0) {
           console.log("will copy");
           $scope.CopyFiles(destination);
       }
    else  {
           console.log("will move");
           $scope.MoveFiles(destination);
       }                               
    };
    

    【讨论】:

      【解决方案3】:

      如果你想确保它是一个包含至少一个元素的数组,请创建一个小函数来检查它。 (也许您稍后会想扩展该检查)

      var isNonEmptyArray = function(ar){
        return Array.isArray(ar) && (ar.length > 0);
      };
      
      $scope.doPaste=function(destination){
      
         if( isNonEmptyArray($scope.selectCopy.ids) ){
           console.log("will copy");
           $scope.CopyFiles(destination);
         }
         if( isNonEmptyArray($scope.selectMove.ids) ){
           console.log("will move");
           $scope.MoveFiles(destination);
          }
      };
      

      同时避免使用弱!= 运算符,使用严格的!==

      [] 比较没有帮助,[] 将始终返回一个新数组。

      【讨论】:

        【解决方案4】:

        注意[] != []返回true(因为它们是不同的对象)。

        您应该使用length 来检查数组是否为空。

        if($scope.selectCopy.ids.length > 0){
             console.log("will copy");
             $scope.CopyFiles(destination);
        }
        

        【讨论】:

        • 这里是一个很好的参考,说明什么是正确的,什么不是正确的:quirksmode.org/js/boolean.html
        • 我认为代码必须是防弹的,所以应该对if(angular.isObject($scope.selectCopy.ids) && $scope.selectCopy.ids.length > 0) { }
        【解决方案5】:

        您必须检查空值或未定义值。

        $scope.doPaste=function(destination) {
           if ($scope.selectCopy.ids && $scope.selectCopy.ids.length > 0) {
               console.log("will copy");
               $scope.CopyFiles(destination);
           }
           if ($scope.selectMove.ids && $scope.selectMove.ids.length > 0) {
               console.log("will move");
               $scope.MoveFiles(destination);
           }                               
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-31
          • 2013-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多