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 146191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:33:29+00:00 2026-05-11T08:33:29+00:00

Ok, so on a web page, I’ve got a JavaScript object which I’m using

  • 0

Ok, so on a web page, I’ve got a JavaScript object which I’m using as an associative array. This exists statically in a script block when the page loads:

var salesWeeks = {     '200911' : ['11 / 2009', 'Fiscal 2009'],     '200910' : ['10 / 2009', 'Fiscal 2009'],     '200909' : ['09 / 2009', 'Fiscal 2009'],     '200908' : ['08 / 2009', 'Fiscal 2009'],     '200907' : ['07 / 2009', 'Fiscal 2009'],     '200906' : ['06 / 2009', 'Fiscal 2009'],     '200905' : ['05 / 2009', 'Fiscal 2009'],     '200904' : ['04 / 2009', 'Fiscal 2009'],     '200903' : ['03 / 2009', 'Fiscal 2009'],     '200902' : ['02 / 2009', 'Fiscal 2009'],     '200901' : ['01 / 2009', 'Fiscal 2009'],     '200852' : ['52 / 2008', 'Fiscal 2009'],     '200851' : ['51 / 2008', 'Fiscal 2009'] }; 

The order of the key/value pairs is intentional, as I’m turning the object into an HTML select box such as this:

<select id='ddl_sw' name='ddl_sw'> <option value=''>== SELECT WEEK ==</option> <option value='200911'>11 / 2009 (Fiscal 2009)</option> <option value='200910'>10 / 2009 (Fiscal 2009)</option> <option value='200909'>09 / 2009 (Fiscal 2009)</option> <option value='200908'>08 / 2009 (Fiscal 2009)</option> <option value='200907'>07 / 2009 (Fiscal 2009)</option> <option value='200906'>06 / 2009 (Fiscal 2009)</option> <option value='200905'>05 / 2009 (Fiscal 2009)</option> <option value='200904'>04 / 2009 (Fiscal 2009)</option> <option value='200903'>03 / 2009 (Fiscal 2009)</option> <option value='200902'>02 / 2009 (Fiscal 2009)</option> <option value='200901'>01 / 2009 (Fiscal 2009)</option> <option value='200852'>52 / 2008 (Fiscal 2009)</option> <option value='200851'>51 / 2008 (Fiscal 2009)</option> </select> 

…with code that looks like this (snipped from a function):

var arr = []; arr.push(     '<select id=\'ddl_sw\' name=\'ddl_sw\'>' +     '<option value=\'\'>== SELECT WEEK ==</option>' );  for(var key in salesWeeks) {     arr.push(         '<option value=\'' + key + '\'>' +         salesWeeks[key][0] + ' (' + salesWeeks[key][1] + ')' +         '<\/option>'     ); }  arr.push('<\/select>');  return arr.join(''); 

This all works fine in IE, FireFox and Opera.

However in Chrome, the order comes out all weird:

<select id='ddl_sw' name='ddl_sw'> <option value=''>== SELECT WEEK ==</option> <option value='200852'>52 / 2008 (Fiscal 2009)</option> <option value='200908'>08 / 2009 (Fiscal 2009)</option> <option value='200906'>06 / 2009 (Fiscal 2009)</option> <option value='200902'>02 / 2009 (Fiscal 2009)</option> <option value='200907'>07 / 2009 (Fiscal 2009)</option> <option value='200904'>04 / 2009 (Fiscal 2009)</option> <option value='200909'>09 / 2009 (Fiscal 2009)</option> <option value='200903'>03 / 2009 (Fiscal 2009)</option> <option value='200905'>05 / 2009 (Fiscal 2009)</option> <option value='200901'>01 / 2009 (Fiscal 2009)</option> <option value='200910'>10 / 2009 (Fiscal 2009)</option> <option value='200911'>11 / 2009 (Fiscal 2009)</option> <option value='200851'>51 / 2008 (Fiscal 2009)</option> </select> 

NOTE: This order, though weird, does not change on subsequent refreshes. It’s always in this order.

So, what is Chrome doing? Some optimization in how it processes the loop?

In the first place, am I wrong to rely on the order that the key/value pairs are declared in any associative array?

I never questioned it before, I just assumed the order would hold because this technique has always worked for me in the other browsers. But I suppose I’ve never seen it stated anywhere that the order is guaranteed. Maybe it’s not?

Any insight would be awesome. Thanks.

  • 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. 2026-05-11T08:33:30+00:00Added an answer on May 11, 2026 at 8:33 am

    Think of an associative array as a paper sack into which all the key-value pairs are placed. As you reach your hand into the sack to look at the pairs (such as with the for…in loop), the order that you encounter them is for all practical purposes random.

    If you want to see them in a specific order, you’ll need to extract the keys into an array and sort it. Then walk across the array, using the keys you encounter to index into the associative array.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use default null. In SQL, null is very different from… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer Javascript runs in a single-thread, so if you have massive… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer If the REST server and the scheduled jobs have nothing… May 11, 2026 at 11:35 pm

Related Questions

Ok, so on a web page, I've got a JavaScript object which I'm using
I need to create a table on a web page that has two rows.
Routine maintenance on a website often involves verifying that links are valid, flagging bad
We have a requirement that has had me skimming the web for quite sometime
The Back Story I have some decimal values which I am displaying as strings

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.