admin管理员组

文章数量:1205762

I have this function:

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

In my app, $scope.selectMove.ids and $scope.selectCopy.ids can't be both non-empty. I mean for example when $scope.selectMove.ids is non-empty $scope.selectCopy.ids is empty.

My problem is that in the console, I always see both will copy and will move.

I have this function:

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

In my app, $scope.selectMove.ids and $scope.selectCopy.ids can't be both non-empty. I mean for example when $scope.selectMove.ids is non-empty $scope.selectCopy.ids is empty.

My problem is that in the console, I always see both will copy and will move.

Share Improve this question edited Apr 23, 2015 at 16:21 Yuri asked Apr 23, 2015 at 15:55 YuriYuri 4473 gold badges9 silver badges20 bronze badges 1
  • 3 You better check .length property! – Dhaval Marthak Commented Apr 23, 2015 at 15:57
Add a comment  | 

5 Answers 5

Reset to default 10

Note [] != [] return true (because they are different objects).

You should use length to check whether an array is empty.

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

I think you should check by angular.isObject() which would return true if it is an object.

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

You have to check for null or undefined values.

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

May be you need to use if else condition:

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

in your code. it is some like this:

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

If you want to make sure it's an Array with at least one element inside, make a small function for checking that. ( maybe you'll want to extend that check later )

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

Also avoid the weak != operator, use the strict one !==.

And comparing to [] is not helpful, [] will always return a new Array.

本文标签: javascriptConditional statement to check if an array is empty with angular JSStack Overflow