给出以下文字:
"rating":{
"overall": 89,
"atmosphere": 82,
"cleanliness": 91,
"facilities": 86,
"staff": 94,
"security": 91,
"location": 91,
"valueForMoney": 90
},
以下for/in
循环:
for (key in data.rating) {
if ( key != "overall" ){
//<div class='ratingbar'><p>" + translateRating(language, key) + "<span class='right bold'>" + data.rating[key]/10 + "</span></p><div class='bar-wrapper'><div class='bar' style='width:" + data.rating[key] + "%'></div></div></div>
//current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, key) + "</span><span class='ratingpercent bold'>" + data.rating[key]/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating[key] + "%'></span></div></li>");
if ( key == "valueForMoney" ){
current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, 'valueForMoney') + "</span><span class='ratingpercent bold'>" + data.rating[key]/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating[key] + "%'></span></div></li>");
}
if ( key == "security" ){
current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, 'security') + "</span><span class='ratingpercent bold'>" + data.rating[key]/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating[key] + "%'></span></div></li>");
}
if ( key == "location" ){
current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, 'location') + "</span><span class='ratingpercent bold'>" + data.rating[key]/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating[key] + "%'></span></div></li>");
}
}
}
当我打印对象的值时,我希望它们以特定的顺序打印 - 与if语句的顺序相同。相反,“valueForMoney”永远是最后的。我期待的顺序是:
答案 0 :(得分:3)
不保证对象中的键有序;你需要使用一个数组或一个Map来确保它们的顺序正确。
如果可以,将数据结构转换为对象数组是最简单的解决方案:
"rating": [
{ name: "overall", value: 89 },
{ name: "atmosphere", value: 82 },
...
]
因为您可以按照保证的顺序遍历它们。
你也可以尝试converting the object to a Map,虽然地图只存在于ES6中,所以如果你已经需要进行转换,那么你就不需要了解。 order of keys in Maps保证是插入的顺序,因此这适用于您的情况。
答案 1 :(得分:2)
JavaScript不保证属性顺序。
ECMAScript 3中对象的定义
4.3.3对象
对象是Object类型的成员。 它是一个无序的属性集合,每个属性都包含一个原始值,对象或函数。存储在对象属性中的函数称为方法。
ECMAScript 6
9.1.11 [[Enumerate]]()
当调用O的[[Enumerate]]内部方法时,将执行以下步骤:
- 返回一个Iterator对象(25.1.1.2),其下一个方法迭代O的可枚举属性的所有字符串值键.Iterator对象必须从%IteratorPrototype%(25.1.2)继承。 未指定枚举属性的机制和顺序,但必须符合下面指定的规则。
醇>
答案 2 :(得分:1)
如果没有用于中的密钥,只需打印这样的内容吗?
current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, 'valueForMoney') + "</span><span class='ratingpercent bold'>" + data.rating.valueForMoney/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating.valueForMoney + "%'></span></div></li>");
current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, 'security') + "</span><span class='ratingpercent bold'>" + data.rating.security/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating.security + "%'></span></div></li>");
current.find('.ratings ul').append("<li class='left'><span class='ratinglabel'>" + translateRating(language, 'location') + "</span><span class='ratingpercent bold'>" + data.rating.location/10 + "</span><div class='ratingbackground rounded'><span style='width:" + data.rating.location + "%'></span></div></li>");