尝试这样的事情:
var data = [{"Body Weight":114," Untrained": 80," Novice": 145," Intermediate": 175," Advanced": 240," Elite": 320},
{"Body Weight":123," Untrained": 85," Novice": 155," Intermediate": 190," Advanced": 260," Elite": 345},
{"Body Weight":132," Untrained": 90," Novice": 170," Intermediate": 205," Advanced": 280," Elite": 370}];
var x = 140,
difference = 0,
bestIndex = 0,
bestDifference = Infinity,
i, cur, bodyWeight;
for (i = 0; i < data.length; i++) {
cur = data[i];
bodyWeight = cur["Body Weight"];
difference = Math.abs(x - bodyWeight);
if (difference < bestDifference) {
bestDifference = difference;
bestIndex = i;
}
}
console.log(data[bestIndex]);
演示:http: //jsfiddle.net/wuSux/2/
更改 的值x
以找到最接近该数字的值。