這裏是我的解決方案
http://jsfiddle.net/7GCu7/131/
var xpos = [];
var ypos = [];
var items = [
{"id" : "1", "xpos":123, "ypos" : 321},
{"id" : "2", "xpos":456, "ypos" : 654},
{"id" : "3", "xpos":789, "ypos" : 987}
];
$.each(items, function(key, value){
xpos.push(value.xpos);
ypos.push(value.ypos);
});
console.log('heighest xpos:' + Math.max.apply(Math, xpos));
console.log('heighest ypos:' + Math.max.apply(Math, ypos));
想出了一個更好的解。這會給你一個包含整個對象的變量,而不僅僅是數字。
http://jsfiddle.net/7GCu7/132/
var xposObj = {"id":"0", "xpos":0, "ypos":0};
var yposObj = {"id":"0", "xpos":0, "ypos":0};
var items = [
{"id" : "1", "xpos":123, "ypos" : 321},
{"id" : "2", "xpos":456, "ypos" : 654},
{"id" : "3", "xpos":789, "ypos" : 987}
];
$.each(items, function(key, value){
if(value.xpos > xposObj.xpos) xposObj = value;
if(value.ypos > yposObj.ypos) yposObj = value;
});
console.log(xposObj);
console.log(yposObj);
我覺得我喜歡這個版本,我的代碼已經通過項目循環並找到了最低/最高值。這似乎是一種更簡化的方法。 –