说你的阵列是这样的:
var persons = [{Name:"John",Age:12},{Name:"Joe",Age:5}];
那么你可以:
var min = Math.min.apply(null, persons.map(function(a){return a.Age;}))
,max = Math.max.apply(null, persons.map(function(a){return a.Age;}))
[编辑]增加ES2015方法:
const minmax = (someArrayOfObjects, someKey) => {
const values = someArrayOfObjects.map(value => value[someKey]);
return {
min: Math.min.apply(null, values),
max: Math.max.apply(null, values)
};
};
console.log(
minmax(
[ {Name: "John", Age: 12},
{Name: "Joe", Age: 5},
{Name: "Mary", Age: 3},
{Name: "James sr", Age: 93},
{Name: "Anne", Age: 33} ],
'Age')
);
[?你尝试过什么(http://whathaveyoutried.com) – 2012-08-16 10:23:00
的[jQuery的最小可能重复/从数组元素的最大属性](http://stackoverflow.com/questions/5052673/jquery-min-max-property-from-array-of-elements) – 2012-08-16 10:23:26
可能的重复[比较JavaScript的对象数组以获取最小/马克斯(http://stackoverflow.com/questions/8864430/compar e-javascript-array-of-objects-to-get-min-max) – 2012-08-16 10:23:43