如何从角js中的json获取最小值和最大值

Ran*_*h M 7 angularjs

我想从角js json中的json获得最大值和最小值:

{

"data":[

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

       ]

}
Run Code Online (Sandbox Code Playgroud)

和我的控制器: -

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

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

};
});
Run Code Online (Sandbox Code Playgroud)

我想从数组中获取最小值和最大值,并将其存储在变量min和max中

Shb*_*Shb 16

你可以简单地使用

$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;}));
Run Code Online (Sandbox Code Playgroud)