关联数组/键对数组在Javascript中按键的随机顺序排列. [英] Associate Array / Key Pair Array In Javascript arranged in with random order of keys.

查看:105
本文介绍了关联数组/键对数组在Javascript中按键的随机顺序排列.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以随机的密钥顺序排列关联阵列/密钥对阵列.每当我添加一个新元素时,javascript就会自动对键进行排序.

Is it possible to have an Associate Array / Key Pair Array arranged in with random order of keys. Whenever, I am adding a new element, javascript is automatically sorting the keys.

例如,

当我以

a[T1001] ="B"
a[T1005] = "A"
a[T1003] ="C"

然后验证了数组列表,我看到该数组自动按以下顺序排序:

and later the array list is verified, I'm seeing that the array is automatically ordered as :

a[T1001]
a[T1003]
a[T1005] 

是否可以保留将值分配给数组的相同顺序?我不能使用Push方法.它会创建一个索引数组,并添加各个对象作为其值.

Is it possible to retain the same order in which I am assigning the values to the array ?. I cannot use the Push method.. It creates an indexed array with the individual objects added as its values.

推荐答案

在我看来,就像您在使用对象一样.永远不能保证对象属性的排序.我认为Internet Explorer(至少是旧版本)不会对属性进行排序,但chrome在默认情况下会进行排序.

Looks to me as if your using an object. The sorting of an object's properties is never guaranteed. I don't think internet explorer (at least the old versions) sorts the properties but chrome does by default.

您的代码无效,如果它们不是数字,则应具有字符串形式的属性.而且您将不得不使用一个对象

Your code is invalid, it should have the properties as strings if they are not numbers. And you would have to use an object

a = {};
a["T1001"] ="B"
a["T1005"] = "A"
a["T1003"] ="C"

如果要保留顺序,可以使用两个数组存储属性和值,或者如果坚持使用对象,则不能保证顺序.

You can either use two arrays to store your properties and the value if you want to preserve the ordering, or you cannot guarantee the ordering if you insist on using an object.

两个数组

a1 = []; a2 = []
a1[0] = "T1001"; a2[0] = "B";
a1[1] = "T1005"; a2[1] = "A";
a1[2] = "T1003"; a2[2] = "C"; 

除非我猜对您的意思是错误的,并且T1001等是包含数字的变量.

Unless I guessed at what you meant wrong and that T1001, etc are variables containing numbers.

这篇关于关联数组/键对数组在Javascript中按键的随机顺序排列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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