如何将变量与Array中的条目进行比较

时间:2011-02-22 19:40:18

标签: actionscript actionscript-2

这是我的问题。

我在flash中做了一个小天气网络应用程序。 所以我读了一个xml feed并从中插入一个数据数组。

我的xml就像这样

<xml>
<prevision>
<date>22</date>
<hour>5</hour>
<temperature>40</temperature>
</prevision>
<prevision>
<date>22</date>
<hour>10</hour>
<temperature>44</temperature>
</prevision>
<prevision>
<date>22</date>
<hour>14</hour>
<temperature>45</temperature>
</prevision>
<prevision>
<date>22</date>
<hour>20</hour>
<temperature>37</temperature>
</prevision>
</xml>

所以这就是我正在使用的ActionScript 2

//**************************************
// Here i'm getting the current hour

var mytime = new Date();
var currenthour = mytime.getHours();

//*************************************
// Here i'm getting my XML

var myhour:Array = new Array();
var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function(success)
{
dataextractor = myxml.firstChild.childNodes;
for (var j = 0; j < dataextractor.length; j++)
{
myhour.push(dataextractor[j].childNodes[1].firstChild.nodeValue);
}
// Doing this
trace(mydate);
}
// Will return this
// 5 , 10,  14 , 20

现在我要做的是找到并跟踪mydate数组中的条目,该条目的值最接近“currenthour”变量(这是我当前的时间我正在变高)。

例如,如果currenthour = 11 在我的myhour数组等于5,10,14,20的情况下 最好的匹配将是myhour [1]

有人可以帮忙吗? myhour数组没有固定数量的条目。 有时它可能是5,10,14和其他时间只有14,20等

非常感谢,

2 个答案:

答案 0 :(得分:0)

如果我明白了,我不确定。你需要找到myhour数组中当前时间的最近数字吗?

就是这样:

var minDiff : int = 12;
var diff : int;
var index : int = 0;   

for (var i : int = 0; i < myhour.length; i++)
{
    diff = Math.abs(myhour[i] - currenthour);
    if (diff > 12)
        diff = 24 - diff;
    if (diff < minDiff)
    {
        minDiff = diff;
        index = i;
    }
}

myhour[index]就是结果。希望,我有个主意。

答案 1 :(得分:0)

想象你的数字在一条线上,从0到24。 使用变量来跟踪XML中当前小时和每小时之间的最小距离,您可以找到您正在寻找的节点的索引:

import mx.xpath.XPathAPI;

var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function(loaded:Boolean) {
    if (loaded) {
        var hoursMin:Number = 24;
        var hoursNow:Number = new Date().getUTCHours();
        var hourID:Number;
        var hours:Array = XPathAPI.selectNodeList(this.firstChild, "xml/prevision/hour");
        var hoursNum:Number = hours.length;
        for(var i:Number = 0 ; i < hoursNum; i++){
            var hoursDiff:Number = Math.abs(hoursNow - parseInt(hours[i].firstChild));//look for the 'shortest distance' within 24 numbers
            if(hoursDiff < hoursMin){//found the smallest current value
                hoursMin = hoursDiff;//update the minimum
                hourID = i;//store the node ID
            }
        }
        trace(this.firstChild.childNodes[hourID]);//access the closest node in time
    } else {
        trace("XML Load Error!!");
    }
}
myxml.load("feed.xml");

没有XPath,这是同样的事情,虽然我发现以这种方式访问​​节点有点困难:

var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function(loaded:Boolean) {
    if (loaded) {
        var hoursMin:Number = 24;
        var hoursNow:Number = new Date().getUTCHours();
        var hourID:Number;
        var hoursNum:Number = this.firstChild.childNodes.length;
        for(var i:Number = 0 ; i < hoursNum; i++){
            var hoursDiff:Number = Math.abs(hoursNow - parseInt(this.firstChild.childNodes[i].childNodes[1].firstChild));//look for the 'shortest distance' within 24 numbers
            if(hoursDiff < hoursMin){//found the smallest current value
                hoursMin = hoursDiff;//update the minimum
                hourID = i;//store the node ID
            }
        }
        trace(this.firstChild.childNodes[hourID]);//access the closest node in time
    } else {
        trace("XML Load Error!!");
    }
}
myxml.load("feed.xml");

HTH