我在ajax成功中有以下内容:
var arrayRating = [];
var arrayRating2 = [];
var hnumb = 0;
var highestProp;
for (var key in json.rating) {
if ( json.rating[key] > hnumb )
{
hnumb = json.rating[key]/10;
highestProp = key;
}
arrayRating.push(hnumb);
}
var higestNumb = Math.max.apply(null, arrayRating);
其中higestNumb给出了“arrayRating”中最高的数字。
我想访问匹配下面案例中最高编号的属性我希望将“位置”存储在变量中。
我尝试过如下:
arrayRating2.push(json.rating);
console.log(arrayRating2)返回以下内容:
[Object]
0: Object
atmosphere: 72
cleanliness: 75
facilities: 69
location: 94
overall: 79
security: 84
staff: 83
valueForMoney: 77
答案 0 :(得分:0)
This works ;)
var obj = {
a: 2,
b: 3,
c: 7,
d: 1,
e: 12,
f: -4,
g: -2
}
function findHighestProp(obj) {
var highest = 0;
Object.keys(obj).forEach(function(key) {
var prop = obj[key];
if (prop > highest) highest = prop;
});
return highest;
}
console.log(findHighestProp(obj)); // 12