Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the min and max value from json in angular js

Tags:

angularjs

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

like image 251
Ranjith M Avatar asked Jan 28 '16 10:01

Ranjith M


1 Answers

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;}));
like image 131
Shb Avatar answered Oct 15 '22 14:10

Shb