comparing the two arrays of objects is not working using angularjs

I am trying to compare to arrays of objects using angularjs. I have tried using angular.equals and no luck. I even assigned them to a variable to compare(initially they weren't) and it still does not detect them. I want to see if the arrays match. if they do--alert and/or reflect true in the {{equals}}

<!DOCTYPE html>
<html ng-app>

    <script src=""></script>
    <script type='text/javascript'>
        function MyCtrl($scope) {

            $scope.thePath = {};
            $scope.thePath2 = {};

            $scope.clickme = function(val) {
                if (val in $scope.thePath) {
                    alert("already present");

                $scope.thePath[val] = 1;


            $scope.clickme2 = function(val) {
                if (val in $scope.thePath2) {
                    alert("already present");

                $scope.thePath2[val] = 1;

            var obj1 = $scope.thePath;
            var obj2 = $scope.thePath2;
            //$scope.thepath vs $scope.thepath2
            $scope.thePath = obj1;
            $scope.thePath2 = obj2;
            $scope.equals = angular.equals(obj1, obj2);

            $scope.doubleCheck = function() {
                alert(obj1, obj2);



    <div ng-app ng-controller="MyCtrl">
        <p>Section A</p>
        <button id="uno" ng-click="clickme('Step A')">Path A</button><br>
        <button id="dos" ng-click="clickme('Step B')">Path B</button><br>
        <button is="cuatro" ng-click="clickme('Step C')">Path C</button><br>
        <button id="cinco" ng-click="clickme('Step D')">Path D</button><br>
        <button id="sieta" ng-click="clickme('Step E')">Path E</button> {{thePath}}

        <p>Section B</p>
        <button id="uno" ng-click="clickme2('Step A')">Path A</button><br>
        <button id="dos" ng-click="clickme2('Step B')">Path B</button><br>
        <button is="cuatro" ng-click="clickme2('Step C')">Path C</button><br>
        <button id="cinco" ng-click="clickme2('Step D')">Path D</button><br>
        <button id="sieta" ng-click="clickme2('Step E')">Path E</button> {{thePath2}}
    <p>Do these match?</p><button ng-click="doubleCheck()">Check</button> {{equals}}


1 answer

  • answered 2018-02-13 00:42 Ronnie

    angular.equals() wont compare the order of the keys in the object. What you could do is JSON.stringify(obj1) === JSON.stringify(obj2)