大家晚上好! 任何人都知道如何从数组中提取前5个数字(最高)? 我有一个带有姓名,姓氏和分数的XML,并希望提取前5个来创建图表。 至于现在,我以这种方式得到所有信息:
num = xmlData.childNodes.length;
for (var i = 0; i <= num - 1; i++)
{
names[i] = this.childNodes[i].childNodes[6].firstChild.nodeValue;
surnames[i] = this.childNodes[i].childNodes[5].firstChild.nodeValue;
points[i] = this.childNodes[i].childNodes[0].firstChild.nodeValue;
pointsint[i] = parseInt(speeds[i]);
trace(i + "." + pointsint[i] + " (" + names[i] + " " + surnames[i] + ")");
}
感谢您的帮助!
答案 0 :(得分:0)
将数据组织为通用对象条目和 Array.sortOn(...)(http://help.adobe.com/en_US/as2/reference/flashlite/WS5b3ccc516d4fbf351e63e3d118ccf9c47f-7ea5.html)。我没有测试它,但我认为这个想法很清楚:
var num = xmlData.childNodes.length;
var entries = [];
for (var i = 0; i <= num - 1; i++)
{
var anEntry = {};
var aNode = xmlData.childNodes[i];
anEntry['name'] = aNode.childNodes[6].firstChild.nodeValue;
anEntry['surname'] = aNode.childNodes[5].firstChild.nodeValue;
var aSpeed = aNode.childNodes[0].firstChild.nodeValue;
anEntry['speed'] = parseInt(aSpeed);
entries.push(anEntry);
trace(i + "." + anEntry['speed'] + " (" + anEntry['name'] + " " + anEntry['surname'] + ")");
}
entries.sortOn("speed", Array.DESCENDING | Array.NUMERIC);
// Now first five elements of entries Array is what you want.