Connecting Tech Pros Worldwide Help | Site Map

Help. Finding max value in array of objects.

timothytoe
Guest
 
Posts: n/a
#1: Feb 6 '08
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.
Joost Diepenmaat
Guest
 
Posts: n/a
#2: Feb 6 '08

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.
timothytoe
Guest
 
Posts: n/a
#3: Feb 6 '08

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:
Situation:
>
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.
Joost Diepenmaat
Guest
 
Posts: n/a
#4: Feb 6 '08

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.
Michael White
Guest
 
Posts: n/a
#5: Feb 7 '08

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.
timothytoe
Guest
 
Posts: n/a
#6: Feb 7 '08

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.
Joost Diepenmaat
Guest
 
Posts: n/a
#7: Feb 7 '08

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.
Michael White
Guest
 
Posts: n/a
#8: Feb 7 '08

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
timothytoe
Guest
 
Posts: n/a
#9: Feb 7 '08

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.

AKS
Guest
 
Posts: n/a
#10: Feb 7 '08

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;

AKS
Guest
 
Posts: n/a
#11: Feb 7 '08

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
timothytoe
Guest
 
Posts: n/a
#12: Feb 7 '08

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:
Situation:
>
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.
Joost Diepenmaat
Guest
 
Posts: n/a
#13: Feb 8 '08

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/
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#14: Feb 10 '08

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
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#15: Feb 10 '08

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
Dr J R Stockton
Guest
 
Posts: n/a
#16: Feb 10 '08

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.
Closed Thread