0

如果我有两个数组,如下例所示,如何使用 'search' 数组中的值搜索 'score' 数组中每个节点的第一部分,并返回 ' 中每个节点的第二部分的值分数'数组?基本上在这种情况下,我想得到 5 和 7。

var score:Array = new Array(); 
score[0] = ["cat", "3"];
score[1] = ["dog", "5"];
score[2] = ["fish", "0.5"];
score[3] = ["bird", "0.25"];
score[4] = ["horse", "10"];
score[5] = ["cow", "15"];
score[6] = ["iguana", "7"];

var search:Array = ["dog", "iguana"];
4

1 回答 1

0

尝试使用此代码(使用字典):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            private var score:Dictionary = new Dictionary();
            private var search:Array = ["dog", "iguana"];

            public function init():void{
                score["cat"] = "3";
                score["dog"] = "5";
                score["fish"] = "0.5";
                score["bird"] = "0.25";
                score["horse"] = "10";
                score["cow"] = "15";
                score["iguana"] = "7";

                var t1:String = score[search[1]];
                var t2:String = score[search[0]];
                Alert.show(t1 + ' ' + t2); //prints 5 7
            }
        ]]>
    </mx:Script>
</mx:Application>

或者你也可以这样做:

            var t1:String = score["dog"];
            var t2:String = score["iguana"];

或没有字典:

<?xml version="1.0" encoding="utf-8"?>

        public function init():void{
            score[0] = ["cat","3"];
            score[1] = ["dog","5"];
            score[2] = ["fish","0.5"];
            score[3] = ["bird","0.25"];
            score[4] = ["horse","10"];
            score[5] = ["cow","15"];
            score[6] = ["iguana","7"];

            for(var j:int = 0;j<search.length;j++){
                for(var i:int = 0;i<score.length;i++){
                    var temp:Array = score[i] as Array;
                    if(temp[0] == search[j]){
                        Alert.show(temp[1]);
                    }
                }
            }
        }
    ]]>
</mx:Script>

此外,您还没有使用适当的方法来声明数组,我建议使用我的方法或更改数组声明。

于 2013-06-11T18:35:15.950 回答