T
timothytoe
Situation:
I have an array of objects. I want to find the maximum value of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called "stat" and the numeric value I want to
check is called "member".
Obviously, a for loop would do it:
var maximum=stat.member[0];
for (i=1;i<stat.length;i++) {
if (stat.member>maximum) {
maximum=stat.member;
}
}
But is there a better way? I know how to bend Math.max to my will to
have is find the max value of an array. Could it be forced into
servitude to solve this case? Or perhaps there's a relatively clean
way to use sort here?
Ideally, I'd want to have a function that takes the name of the
element I want to check.
maximum=getMax(stat,"member");
Looking for a clear, compact solution. Clever is optional.
I have an array of objects. I want to find the maximum value of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called "stat" and the numeric value I want to
check is called "member".
Obviously, a for loop would do it:
var maximum=stat.member[0];
for (i=1;i<stat.length;i++) {
if (stat.member>maximum) {
maximum=stat.member;
}
}
But is there a better way? I know how to bend Math.max to my will to
have is find the max value of an array. Could it be forced into
servitude to solve this case? Or perhaps there's a relatively clean
way to use sort here?
Ideally, I'd want to have a function that takes the name of the
element I want to check.
maximum=getMax(stat,"member");
Looking for a clear, compact solution. Clever is optional.