0

I have an array made of mysql table in angularjs, i return it into json with php and get it in angularjs. I have created an application in ionic and make use of angularjs. I can delete item's, insert items and edit item. Items are having attribute priority, and i want priority to be unique. So on item add i am checking for the last added item.priority and next one has priority + 1. On item delete, i check the item.priority so next added item will have the priority set as the priority of deleted item. But for example i have 6 items and i delete item4 so next added item will have priority 4, but when i add next it's on priority 5, but I already have 5 so i want to have priority set on 7 as i have 6 items. So what i try is :

$scope.priority = Math.max(parseInt($rootScope.obiadki.priority));

But it returns undefined. My question is, how can i check for the max value of priority attribute of all objects in whole array.

How i retrive data :

$scope.getData = function () {
    $http({
        method: 'get',
        url: 'mygetdatacodeurl'
    }).then(function successCallback(response) {
        // Store response data
        $rootScope.obiadki = response.data;
        $scope.checkPriority();
    });
};

How i check for the priority :

$scope.checkPriority = function () {
   $scope.priority = Math.max(parseInt($rootScope.obiadki.priority));

};

How i check priority on addItem :

$scope.checkPriorityOnAdd = function () {
    $scope.priority = parseInt($scope.priority) + 1;
};

my addItem function :

$scope.addItem = function() {
    $http({
        method: "post",
        url: 'myinsertcodeurl',
        data: {
            id: $scope.id,
            obiad_name: $scope.obiad_name,
            active: $scope.active,
            priority: $scope.priority
        },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });
    $scope.getData();
    $scope.checkPriorityOnAdd();
};

my deleteItem function where i set priority on delete :

$scope.onItemDelete = function(item) {
    $http({
        method: "post",
        url: 'mydeletecodeurl',
        data: {
            id: item.id
        },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });
    $scope.getData();
    $scope.priority = item.priority;
};
SupremeDEV
  • 384
  • 1
  • 15

2 Answers2

0

You need to iterate over all elements in your object array, and compare object attribute to a max value initialised to 0 :

   $scope.max = 0 ;

    for (var i = 0; i <= $rootScope.obiadki.length; i++) {
    if ($rootScope.obiadki[i].priority>$scope.max) {
      $scope.max=$rootScope.obiadki[i].priority;
      this.returnObject = $rootScope.obiadki[i];      
     } 
  }
Léo R.
  • 2,620
  • 1
  • 10
  • 22
0

using Lambda

$scope.max = $rootScope.obiadki.reduce((prev, current) => (prev.priority > current.priority) ? prev : current);

using function works in old browsers also

$scope.max = $rootScope.obiadki.reduce(function(prev, current){return (prev.priority > current.priority) ? prev : current;});
RamiReddy P
  • 1,628
  • 1
  • 18
  • 29