Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:55:42+00:00 2026-06-17T06:55:42+00:00

I have an object with keys representing country shortcodes and values representing a count.

  • 0

I have an object with keys representing country shortcodes and values representing a count. I would like to iterate through this object and return an array of countries with the highest counts. I’m new to Coffeescript and unsure of the most elegant way to handle this. Any help is most appreciated. Thanks!

With the below data as an example I would like for the array to return ['AU', 'US', 'BR', 'CN', 'IN']

vacation_spots = {
  AU: 3,
  BR: 2,
  CF: 1,
  CN: 2,
  IN: 2,
  MX: 1,
  SD: 1,
  TD: 1,
  TM: 1,
  US: 3
}

get_top_5(vacation_spots)

get_top_5 = (items) ->
    for k, v of items
    # ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:55:43+00:00Added an answer on June 17, 2026 at 6:55 am

    Using vanilla JS Array methods:

    get_top_5 = (items) ->
      codes = (k for k of items)
      sortedCodes = codes.sort (a, b) -> items[b] - items[a]
      sortedCodes[...5]
    

    You can compress it all into one expression like (k for k of items).sort((a, b) -> items[b] - items[a])[...5] but i think separating each step reads a bit better.

    The sort step sorts the country codes by their value on the items object; it uses the Array::sort method, which expects a comparator function that takes two arguments and returns an integer. If you have Underscore.js included, i’d recommend using _.sortBy, which uses comparator function that just takes one argument and returns a comparable object:

    sortedCodes = _.sortBy codes, (code) -> -items[code]
    

    Edit: Also, instead of (k for k of items) you can use Object.keys(items) (beware, IE <9 will not support it) or _.keys(items), both of which will compile to much more compact JS code than the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that has private keys like this _errors:private . I am
Say I have an array with keys representing id and values representing parent :
I have a Django Queryset object called data containing string keys and float values.
I have a series of object keys: $this->rules->days->mon = isset($this->recurring_event_data['post_ar'][$this->rules->type]['mon']) ? true : false;
I have object properties in my code that look like this: $obj ->field_name_cars[0]; $obj
I want create arrays with object keys in PHP, i.e. something like this: <?php
I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have this object: stdClass Object ( [daily_inventoryID] => 1 [inventory_timestamp] => 2012-06-08 14:35:42
I have an object of objects, which I'd like to sort by property... having
My goal is to have a object like below: var attrs = { price

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.