473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 17489
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen | last post by:
I have been getting on well with help from this forum trying to create an array list and work with it. Everything is working fine apart from displaying my array list items into the labels in my...
2
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to...
3
by: ricolee99 | last post by:
Hi everyone, I have a problem that i have been trying to solve for awhile. I'm given a code where the purpose is to create a general dataset mapper. Given any dataset, i have a class,...
3
by: HEMH6 | last post by:
Who can help solve this problem??? Finding the Largest Value (a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
45
by: davy.zou | last post by:
I have started learning c++ and I need help. I need to write a program, the question is as follows. At a post office, there are a certain number of 2, 7, and 9cents stamps, now, given a total...
5
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.