469,602 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

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.
Feb 6 '08 #1
15 17196
timothytoe <ti********@gmail.comwrites:
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.
Feb 6 '08 #2
On Feb 6, 11:30 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
timothytoe <timothy...@gmail.comwrites:
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...5_Reference:Ob...

Joost.
Oh. That's cool. Probably only worth it if I would want to use reduce
in more places than one.
Feb 6 '08 #3
timothytoe <ti********@gmail.comwrites:
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.
Feb 6 '08 #4
timothytoe wrote:
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
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.
Feb 7 '08 #5
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.
Feb 7 '08 #6
timothytoe <ti********@gmail.comwrites:
>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.
Feb 7 '08 #7
timothytoe wrote:
>>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
Feb 7 '08 #8
On Feb 6, 5:40 pm, Michael White <m...@mickweb.comwrote:
timothytoe wrote:
>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
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.

Feb 7 '08 #9
AKS
On Feb 6, 11:10 pm, timothytoe <timothy...@gmail.comwrote:
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;

Feb 7 '08 #10
AKS
On Feb 7, 11:15 am, AKS <aksus...@yandex.ruwrote:
alert(Math.min.apply(null, stat)); // =20
^^^^^^^^
Math.max
Feb 7 '08 #11
On Feb 6, 10:15 pm, AKS <aksus...@yandex.ruwrote:
On Feb 6, 11:10 pm, timothytoe <timothy...@gmail.comwrote:
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.
Feb 7 '08 #12
Dr J R Stockton <jr*@merlyn.demon.co.ukwrites:
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.
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/
Feb 8 '08 #13
Dr J R Stockton wrote:
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
Feb 10 '08 #14
timothytoe wrote:
On Feb 6, 10:15 pm, AKS <aksus...@yandex.ruwrote:
>On Feb 6, 11:10 pm, timothytoe <timothy...@gmail.comwrote:
>>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
Feb 10 '08 #15
In comp.lang.javascript message <47**************@PointedEars.de>, Sun,
10 Feb 2008 14:29:25, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>
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.
Feb 10 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by farseer | last post: by
3 posts views Thread by ricolee99 | last post: by
45 posts views Thread by davy.zou | last post: by
5 posts views Thread by Stephen3776 | last post: by
1 post views Thread by =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.