Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


getting top five from array

getting top five from array

getting top five from array

I have a function that gets the number of submitted tickets for each user. How do I make this get the top five submitters (and their respective ticket totals) and lump the rest of the count into "other." This data is being pulled from a database. thanks!


var user_burt_count = [];
for ( var i=0; i < data.length; i++ ) {            
                var submitter = data[i].sub_by;
                if( !( submitter in user_burt_count ) ) {
                                user_burt_count[submitter] = 1;
                else {

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.

RE: getting top five from array


Well just sort them, then sum all starting at 5th item.

In this code you will have the resulting top 5 + 1 elements in object top :

CODE --> JavaScript

var order = Object.keys(user_burt_count).sort(function(a, b) {
  return user_burt_count[b] - user_burt_count[a]

var top = {}, other = 0
for (var i = 0, l = order.length; i < l; i++)
  if (i < 5)
    top[order[i]] = user_burt_count[order[i]]
    other += user_burt_count[order[i]]

if (other)
  top['other'] = other 

See it in action on http://jsfiddle.net/LLkM8/ .

If I misunderstood you, please clarify the requirements.


RE: getting top five from array

perfect, thanks!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close