jQuery对象属性值的平均值 [英] jquery object properties value average
本文介绍了jQuery对象属性值的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下对象:
rating {
atmosphere: 85
cleanliness: 91
facilities: 91
staff: 85
security: 94
location: 78
valueForMoney: 85
}
如何获取所有属性值的平均和?
How can I get the average sum of all of the properties value?
$.each( json.rating, function( key, value) {
console.log( key + ": " + value);
});
推荐答案
您可以遍历对象的属性以获取总值,然后将其除以对象本身中的数字或属性:
You can loop through the object's properties to get the total value, then divide that by the number or properties in the object itself:
var total = 0;
for (var key in json.rating) {
total += json.rating[key];
}
console.log(total / Object.keys(json.rating).length);
这篇关于jQuery对象属性值的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文