5

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.

1
  • 3
    You better check .length property! Commented Apr 23, 2015 at 15:57

5 Answers 5

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Here is a good reference for what does and does not resolve to true: quirksmode.org/js/boolean.html
I think the code must be bullet proof, so should right if(angular.isObject($scope.selectCopy.ids) && $scope.selectCopy.ids.length > 0) { }
6

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

Comments

3

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

Comments

2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.