8

i want to get the maximum and minimum value from a json in angular js my json:

{

"data":[

        {
         "name": "ranjith",
         "age": 22
        },
        {
         "name": "rahul",
         "age": 20
        },
        {
         "name": "jinesh",
         "age": 25
        },
        {
         "name": "vishnu",
         "age": 24
        }

       ]

}

and my controller:-

var app = angular.module('myApp', ['']);
app.controller('myCtrl', function($scope, data) {

$scope.data = data.data;
$scope.min="";
$scope.max="";

};
});

i want to get the minimum and maximum value from the array and store it in the Variables min and max

1
  • 1
    Which of the properties of the object inside the array defines the max value? You'll need to loop through the whole array and search for the max value. O you could sort the array from bigger to lower and the first element will be your maximum value. Any way you have to implement the function to compare the objects inside the array Commented Jan 28, 2016 at 10:31

5 Answers 5

19

You can simply use

$scope.min = Math.min.apply(Math,$scope.data.map(function(item){return item.age;}));

$scope.max = Math.max.apply(Math,$scope.data.map(function(item){return item.age;}));
Sign up to request clarification or add additional context in comments.

1 Comment

for me its day saver answer :)
2

To avoid using 3rd party lib

    for (var index in $scope.data.data)
    {
        var item=$scope.data.data[index];
        if($scope.min==0 && $scope.max==0){
            // set first default element
            $scope.min=item.age;
            $scope.max=item.age;
        }
        else{
            if($scope.min>item.age)
                $scope.min=item.age;
            else if($scope.max<item.age)
                $scope.max=item.age;
        }
    }

Comments

1

You can use Underscore.js with angular and then in your controller,

var ageArr = _.pluck(data.data, 'age');
$scope.min= _.min(ageArr);
$scope.max= _.max(ageArr);

Plunker url http://run.plnkr.co/plunks/Mw531cqUIHVSorb5kOac/

Comments

0

1.Add Underscore dependency

2.Use the following:

//For Maximum Age:
_.max(data, function(itr){ return itr.age; });

/* O/p = {
     "name": "jinesh",
     "age": 25
 }*/

//For Minimum Age:
 _.min(data, function(itr){ return itr.age; });


/* O/p = {
      "name": "rahul",
      "age": 20
    } */

4 Comments

first line "1) Add Underscore dependency" is not valid angular js code
2) Oh My mistake, its not part of code it is just instruction to add underscore dependency in ur controller
Oh ok, I'll discard the first line of your code. But now the second line "2) For Maximum Age:" is giving me similar syntax errors. It also doesn't appear to be valid angular js code.
Also "For Minimum Age" appears to be a for loop but it's not clear how it's managed. Are Minimum and Age variables that control the loop? Are you sure the part formatted as code is actually valid code?
0

If the intent is to display, you can directly arrive at min by this: {{(data | orderBy:'age')[0].age}}

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.