谷歌浏览器:JavaScript 关联数组,乱序评估 [英] Google Chrome: JavaScript associative arrays, evaluated out of sequence

查看:26
本文介绍了谷歌浏览器:JavaScript 关联数组,乱序评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以在网页上,我有一个 JavaScript 对象,我将其用作关联数组.这在页面加载时静态存在于脚本块中:

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"]
};

键/值对的顺序是有意的,因为我将对象转换为 HTML 选择框,例如:

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("");

这在 IE、FireFox 和 Opera 中都可以正常工作.

This all works fine in IE, FireFox and Opera.

但是在 Chrome 中,这个顺序很奇怪:

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.

那么,Chrome 在做什么?它如何处理循环的一些优化?

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.

推荐答案

把关联数组想象成一个纸袋,所有的键值对都放在里面.当您将手伸入袋中查看对(例如 for...in 循环)时,您遇到它们的顺序出于所有实际目的都是随机的.

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.

这篇关于谷歌浏览器:JavaScript 关联数组,乱序评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆