Help. Finding max value in array of objects. | | |
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[i]>maximum) {
maximum=stat.member[i];
}
}
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. | | | | re: Help. Finding max value in array of objects.
timothytoe <timothytoe@gmail.comwrites: Quote:
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[i]>maximum) {
maximum=stat.member[i];
}
}
>
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.
If you have javascript 1.8 (probably not, but see below):
var max = stat.reduce( function(a,b) { return a b ? a : b } );
For older versions: there's a definition of Array.prototype.reduce at http://developer.mozilla.org/en/docs...s:Array:reduce
Joost. | | | | re: Help. Finding max value in array of objects.
On Feb 6, 11:30 am, Joost Diepenmaat <jo...@zeekat.nlwrote: Quote:
timothytoe <timothy...@gmail.comwrites: > Quote:
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".
> Quote:
Obviously, a for loop would do it:
> Quote:
var maximum=stat.member[0];
for (i=1;i<stat.length;i++) {
if (stat.member[i]>maximum) {
maximum=stat.member[i];
}
}
> Quote:
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?
> Quote:
Ideally, I'd want to have a function that takes the name of the
element I want to check.
> Quote:
maximum=getMax(stat,"member");
> Quote:
Looking for a clear, compact solution. Clever is optional.
>
If you have javascript 1.8 (probably not, but see below):
>
var max = stat.reduce( function(a,b) { return a b ? a : b } );
>
For older versions: there's a definition of Array.prototype.reduce at
> http://developer.mozilla.org/en/docs...5_Reference:Ob...
>
Joost.
Oh. That's cool. Probably only worth it if I would want to use reduce
in more places than one. | | | | re: Help. Finding max value in array of objects.
timothytoe <timothytoe@gmail.comwrites: Quote:
Oh. That's cool. Probably only worth it if I would want to use reduce
in more places than one.
Sure, but you it will at least let you implement max(), min(),
average(), total(), all() and none() very easily.
Just keep it in mind if you want to reduce a list of values to a single
value.
Likewise, map and filter are also pretty useful (maybe even more useful): http://developer.mozilla.org/en/docs...ects:Array:map http://developer.mozilla.org/en/docs...s:Array:filter
I use those daily.
Joost. | | | | re: Help. Finding max value in array of objects.
timothytoe wrote: Quote:
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[i]>maximum) {
maximum=stat.member[i];
}
}
maximum= array.sort()[array.length-1]
Mick Quote:
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.
| | | | re: Help. Finding max value in array of objects. Quote:
maximum= array.sort()[array.length-1]
Mick
I don't quite get that. You're ignoring "member." I'm not just looking
for the max in an array. I'm looking for the maximum of an element in
an object in an array.
For that matter, Joost ignored it too. Neither solution works as is. | | | | re: Help. Finding max value in array of objects.
timothytoe <timothytoe@gmail.comwrites: Quote: Quote:
>maximum= array.sort()[array.length-1]
>Mick
>
I don't quite get that. You're ignoring "member." I'm not just looking
for the max in an array. I'm looking for the maximum of an element in
an object in an array.
>
For that matter, Joost ignored it too. Neither solution works as is.
stat.map(
function(m) { return m.member } ).reduce(
function (a,b) { a b ? a : b } );
Joost. | | | | re: Help. Finding max value in array of objects.
timothytoe wrote: Quote: Quote:
>>maximum= array.sort()[array.length-1]
>>Mick
>
>
I don't quite get that. You're ignoring "member." I'm not just looking
for the max in an array. I'm looking for the maximum of an element in
an object in an array.
>
For that matter, Joost ignored it too. Neither solution works as is.
array == stat.member, no?
maximum= stat.member.sort()[stat.member.length-1]
or I'm missing something.
Mick | | | | re: Help. Finding max value in array of objects.
On Feb 6, 5:40 pm, Michael White <m...@mickweb.comwrote: Quote:
timothytoe wrote: Quote: Quote:
>maximum= array.sort()[array.length-1]
>Mick
> Quote:
I don't quite get that. You're ignoring "member." I'm not just looking
for the max in an array. I'm looking for the maximum of an element in
an object in an array.
> Quote:
For that matter, Joost ignored it too. Neither solution works as is.
>
array == stat.member, no?
maximum= stat.member.sort()[stat.member.length-1]
>
or I'm missing something.
Mick
Maybe I'm misunderstanding your solution.
member is not an array. It's a number.
it's an array of objects. Each object has a member.
I see that I screwed up my code, which is how I threw everyone off.
Should have been:
var maximum=stat[0].member;
for (i=1;i<stat.length;i++) {
if (stat[i].member]>maximum) {
maximum=stat[i].member;
}
}
Sorry about that. | | | | re: Help. Finding max value in array of objects.
On Feb 6, 11:10 pm, timothytoe <timothy...@gmail.comwrote: Quote:
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".
var stat = [{ member: -1 }, { member: 1 },
{ member: 20 }, { member: 5 }];
var toString = Object.prototype.toString;
Object.prototype.toString = function () {
return this.member;
};
alert(Math.min.apply(null, stat)); // =20
Object.prototype.toString = toString; | | | | re: Help. Finding max value in array of objects.
On Feb 7, 11:15 am, AKS <aksus...@yandex.ruwrote: Quote:
alert(Math.min.apply(null, stat)); // =20
^^^^^^^^
Math.max | | | | re: Help. Finding max value in array of objects.
On Feb 6, 10:15 pm, AKS <aksus...@yandex.ruwrote: Quote:
On Feb 6, 11:10 pm, timothytoe <timothy...@gmail.comwrote:
> > Quote:
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".
>
var stat = [{ member: -1 }, { member: 1 },
{ member: 20 }, { member: 5 }];
>
var toString = Object.prototype.toString;
>
Object.prototype.toString = function () {
return this.member;
>
};
>
alert(Math.min.apply(null, stat)); // =20
>
Object.prototype.toString = toString;
Ah. Good. I get it. | | | | re: Help. Finding max value in array of objects.
Dr J R Stockton <jrs@merlyn.demon.co.ukwrites: Quote:
Yes. It makes no difference. The important point is that it is not the
browser(s) that "you" have (you, JD, were replying to the OP, who is
presumed to be an author) have that matters ; it is the browsers that
the author's readers have. AISB.
I'm not arguing the technical point, I was just trying to be concise.
IOW I was contracting "the browser you and everybody else who's viewing
the site is using to view the site have javascript version 1.8 or higher
and have it enabled at that time" to "you have javascript 1.8". Yes it's
sloppy, but I think it got the point across in this case. Quote:
The distinction between author's system and readers' systems is too
often forgotten.
Sure. Fair enough.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/ | | | | re: Help. Finding max value in array of objects.
Dr J R Stockton wrote: Quote:
It would be useful if the FAQ were to list, or explicitly link to, a
chronological table showing when the major browsers were released, and
which versions of JavaScript they came with.
Such a list exists already and has been referred to here several times before: http://PointedEars.de/es-matrix
(note the Version Information links)
As for the Language Features, more testing and maybe augmentation is
required. Feedback of the inclined reader is therefore much appreciated.
It should also be noted that for JScript it is possible that an older
version of Internet Explorer supports a newer version of JScript than it was
originally shipped with through an update of the Microsoft Script Engine.
That is not possible, or in the Mozilla.org days at least unlikely, with
JavaScript.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16 | | | | re: Help. Finding max value in array of objects.
timothytoe wrote: Quote:
On Feb 6, 10:15 pm, AKS <aksus...@yandex.ruwrote: Quote:
>On Feb 6, 11:10 pm, timothytoe <timothy...@gmail.comwrote: Quote:
>>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".
>var stat = [{ member: -1 }, { member: 1 },
> { member: 20 }, { member: 5 }];
>>
>var toString = Object.prototype.toString;
>>
>Object.prototype.toString = function () {
> return this.member;
>>
>};
>>
>alert(Math.min.apply(null, stat)); // =20
>>
>Object.prototype.toString = toString;
>
Ah. Good. I get it.
I hope you do, because it does have drawbacks:
- Function.protototype.apply() may not be supported
(JavaScript < 1.3, JScript < 5.5, ECMAScript < 3)
- Math.min() or Math.max() may not support more than two arguments
(JavaScript < 1.5, JScript < 5.5, ECMAScript < 3)
See also http://PointedEars.de/es-matrix#m (updated)
As a workaround for the former,
window.alert(Math.min.apply(null, stat));
is equivalent to
this.min = Math.min;
window.alert(this.min(stat[0], stat[1], stat[2], stat[3]));
delete this.min;
in global context here. A universal workaround for
Function.prototype.apply() could probably only be realized with eval().
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann | | | | re: Help. Finding max value in array of objects.
In comp.lang.javascript message <47AEFC35.9060901@PointedEars.de>, Sun,
10 Feb 2008 14:29:25, Thomas 'PointedEars' Lahn <PointedEars@web.de>
posted: Quote:
>
>However, I think that iterating through the array is in most cases more
>efficient than sorting it and retrieving the last element. The former
>algorithm has a complexity of O(n) in the Worst Case, even Heapsort has
>O(n * log(n)) then. Meaning it is already slower with 3 or more unsorted items.
The last sentence is not a valid deduction from what precedes it.
--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|