1

I have 2 arrays: cars & bikes. I want to compare each element of car array to all the other elements of bike array, If both are equal, then alert true else false.

$scope.getComparison = function() {
$scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
angular.forEach($scope.bikes, function(value, key) {
    $scope.p_Id = value.bikes;
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.cars, function(value, key) {
        $scope.b_Id = value.cars;
        if ($scope.p_Id == $scope.b_Id) {
            alert(b_Id + "=" + p_Id + "=" + "true");
        } else {
            alert(b_Id + "=" + p_Id + "=" + "false");
        }
    });
  });
};

Where am I wrong ? I need help.

1
  • 1
    "value.bikes" ? Check console for errors.. Commented May 20, 2016 at 6:42

7 Answers 7

2

Not working!

var _firstArray = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
var _secondArray=["1", "2", "3", "4", "54", "55", "56", "56", "58"];

var areEqual = true;

_firstArray.forEach(function(item) {
    if (!(item in _secondArray)) {
        areEqual = false;
    }
});
console.log(areEqual);

EDIT: I was wrong about the "in" operator. It only checks properties for a given object.

"45" in _firstArray will result in false, but 0 in _firstArray will result in true as 0 is a property for that array, 'length' in _firstArray will result in true as well. Here is a working solution:

var _firstArray = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
var _secondArray=["1", "2", "3", "4", "54", "55", "56", "56", "58"];

_firstArray.forEach(function(item1) {
    _secondArray.forEach(function (item2) {
        if (item1 === item2) {
            console.log(item1 + '=' + item2 + '=' + 'true');
        } else {
            console.log(item1 + '=' + item2 + '=' + 'false');
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

try this :

   $scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
   $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
   angular.forEach($scope.bikes, function(bike) {
      angular.forEach($scope.cars, function(car) {
        if (bike == car) {
          alert(bike + "=" + car + "=" + "true");
         } else {
             alert(bike  + "=" + car+ "=" + "false");
           }
     });
   });

Comments

1

You can use forEach & indexOf array methods to find the match

var _firstArray = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
var _secondArray=["1", "2", "3", "4", "54", "55", "56", "56", "58"];
_firstArray.forEach(function(item){
  if (_secondArray.indexOf(item) !==-1){
     console.log(item)  
  }
  else{
  console.log("not matched");
  }
})

Note: You can same array methods in your code

Check this jsfiddle

Comments

0

Hope this is what you're looking for

jsFiddle link - https://jsfiddle.net/U3pVM/25016/ . Replaced alert with console

$scope.getComparison = function() {
$scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
angular.forEach($scope.bikes, function(value, key) {
    $scope.p_Id = value;
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.cars, function(value, key) {
        $scope.b_Id = value;
        if ($scope.p_Id == $scope.b_Id) {
            alert($scope.b_Id + "=" + $scope.p_Id + "=" + "true");
        } else {
            alert($scope.b_Id + "=" + $scope.p_Id + "=" + "false");
        }
    });
});
};

Comments

0

value argument of the forEach() method already represents the bike in the array as such, same goes for the car: You can change your code like this:

$scope.getComparison = function() {
$scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
angular.forEach($scope.bikes, function(value, key) {
    $scope.p_Id = value;
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.cars, function(value, key) {
        $scope.b_Id = value;
        if ($scope.p_Id == $scope.b_Id) {
            alert(b_Id + "=" + p_Id + "=" + "true");
        } else {
            alert(b_Id + "=" + p_Id + "=" + "false");
        }
    });
});
};

And it should work.

You can also nest the forEach() methods like such:

 $scope.getComparison = function() {
    $scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.bikes, function(bikeValue, bikeKey) {
        angular.forEach($scope.cars, function(carValue, carKey) { 
            if (bikeValue == carValue) {
                alert(bikeValue + "=" + carValue + "=" + "true");
            } else {
                alert(bikeValue + "=" + carValue + "=" + "false");
            }
        });
    });
    };

This way directly compare them without saving the values to $scope.

Comments

0

Comparing each element of 1 array with each element of another array?

2 each's means use 2 loops thats it.

Comments

0

There were some issue with your code, try below code and should work perfectly:

$scope.getComparison = function() {
                        $scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
                        angular.forEach($scope.bikes, function(value, key) {
                            $scope.p_Id = value;
                            $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
                            angular.forEach($scope.cars, function(value, key) {
                                $scope.b_Id = value;
                                if ($scope.p_Id == $scope.b_Id) {
                                    alert($scope.b_Id + "=" + $scope.p_Id + "=" + "true");
                                } else {
                                    alert($scope.b_Id + "=" + $scope.p_Id + "=" + "false");
                                }
                            });
                        });
                    };

2 Comments

You should probably explain what the issue is.
Issue was with value.bikes & value.cars, actually we had to refer value not value.bikes since it is nothing. So we have corrected it in the above code added by me.

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.