Ranjith M Ranjith M - 13 days ago 10
AngularJS Question

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

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

Shb Shb
Answer

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;}));
Comments