Comparing objects inside array, do i use equals or indexOf -1?

  1. How can i check equality of arrays objects values (lets say, if im trying to add new date and time, which are same as old), when i have array named orderInfo, do i need to use 'indexOf' or 'angular.equals' method?

  2. How i do the checking from array, because these ones pass the if statement?

     $scope.orderInfo = [{date: '11.11.2011', time: 8},
                     {date: '11.11.2005', time: 8}];
    

    if ($scope.orderInfo.indexOf(time) === -1) {

         $scope.orderInfo.push({date: pvmr, time: aikas});
    
         $scope.errortextt = "info added";
    
    
         } else {
         $scope.errortextt = "already on list";
         }
    

or

$scope.containsObject = function (orderInfo, time) {
    var i;
    for (i = 0; i < orderInfo.length; i++) {
        if (angular.equals(orderInfo[i], time)) {


            $scope.errortextt = "already on list";
            return true;
        }
    }
     $scope.orderInfo.push({date: pvmr, time: aikas});

         $scope.errortextt = "info added";
    return false;
};

Answers


indexOf will only work if it's an array of numbers or strings not objects unless you are actually giving it a reference to an object that is in the array (not one with the same values). The angular.equals function will check the equality of the properties of the object individually to determine if they are "equal".

Personally think the best way to solve this particular problem is to just use another object as a "hash map" to determine if you have the value or not, this way no need to loop or do the deep comparison, the only difficulty is in coming up with your unique "key" for storing/checking a value for example:

var orderInfoIndex = {};
$scope.containsObject = function (orderInfo, time) {
  if (orderInfoIndex[time.toString()]) {
    $scope.errortextt = "already on list";
    return true;
  }

  orderInfoIndex[time.toString()] = true;

  $scope.orderInfo.push({date: pvmr, time: aikas});

  $scope.errortextt = "info added";
  return false;
};

You should go by the 2nd method:

angular.equals(obj1,obj2);

I'm using that .containsObject() function to check, that you cant add same date, time more than once. This solution works perfectly with one button, but i have array of buttons like this:

 $scope.times = [8, 9, 10];

html

<ul>
        <li class="items" ng-repeat="time in times">
            <button class="button" ng-click="containsObject(time, date)">{{time}}</button>
        </li>
    </ul>

the 'date' is a momentJs date object.

PROBLEM: When add objects and check equality with containsObject() , it adds items right, till all the 'times'-arrays numbers 'have been added'. then it only push one time/date combo to list per date and says rest, that they are already on list, even when they arent.

$scope.orderInfoIndex{};

$scope.containsObject = function (aikas, pvmr) {
    if ($scope.orderInfoIndex[pvmr] && $scope.orderInfoIndex[aikas]) {
        $scope.errortextt = "already on list";
        return true;



    }
        $scope.orderInfo.push({date: pvmr, clock: aikas});

        $scope.orderInfoIndex[pvmr] = true;
        $scope.orderInfoIndex[aikas] = true;



        $scope.errortextt = "info added";
        return false;

};

EXAMPLE: {date:"2016-09-29T09:00:00.000Z","clock":8}, {date:"2016-09-29T09:00:00.000Z","clock":9}, {date:"2016-09-29T09:00:00.000Z","clock":10} then when i change date(29->30), i can push only one object:

{date:"2016-09-30T09:00:00.000Z","clock":8}

if i try to push other object to same date, with different clock, it doesnt allow and says 'already on list'

QUESTION: 1. How can I get times array work together with date change, so it checks time,date together, depending which date you are selecting?

bonus: How i can reverse/delete specific selection, so i return right dates time-button-value?


Need Your Help

Twitter bootstrap - break-word not work on dropdown-menu

drop-down-menu twitter-bootstrap word-wrap

I applied word-wrap: break-word to twitter bootstrap dropdown-nemu, but it didn't work. Here's the JSFiddle.

Selenium 2 - Can't click on menu

internet-explorer selenium webdriver

I use WebDriver, and everything works great in firefox.