I have an Array like the following, how would I sort it by the keys of the objects it contains?
var myArray = [
item : {
},
anotherItem : {
}
];
EDIT
myArray
is an Object not an Array. Does that change anything?
I have an Array like the following, how would I sort it by the keys of the objects it contains?
var myArray = [
item : {
},
anotherItem : {
}
];
EDIT
myArray
is an Object not an Array. Does that change anything?
首先,该语法是无效的:无法在数组文字中设置键。此外,虽然有可能,但您可能不想要具有非数字属性的数组;数组旨在保存数字属性。
即使这些被纠正,对象中的属性也没有定义的迭代顺序:
未指定枚举属性 […]的机制和顺序。
根据定义,排序会影响顺序;如果一开始就没有定义的顺序,你就不能真正改变它。