`
hereson
  • 浏览: 1454384 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Array中比较有意思的两个方法:filter和foreach

    博客分类:
  • flex
阅读更多

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

<mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml " layout=" absolute " creationComplete="init()" >

    <mx:Script>

       <![CDATA[

      

           /**

             * Test function filter

             * function foreach of Array

             * */

          

           private function init(): void

           {

              var arr:Array= new Array();

              arr.push({label: "A" ,flag: true });

              arr.push({label: "B" ,flag: true });

              arr.push({label: "C" ,flag: false });

              arr.push({label: "D" ,flag: true });

              arr.push({label: "E" ,flag: false });

             

              var arrTemp:Array=arr.filter(flagFilter);

              //way 1

              for each ( var item:* in arrTemp)

              {

                  // 遍历 Object 中的属性名及值

                  for ( var prop:String in item)

                  {

                     trace (prop+ ":" +item[prop]);

                  }

              }

              //way 2

              arrTemp.forEach(traceProp);

           }

          

           private function flagFilter(item:*,i:int ,arr:Array):Boolean

           {

              // 过滤 label 属性值得为 A 的对象,其余的返回 false

              if (item.label== "A" )

                  return true ;

              return false ;

              //or

              //return item.flag;

           }

          

           private function traceProp(element:*, index:int, arr:Array): void {

                trace (index + ":" + element.label + ":" + element.flag);

            }

       ]]>

    </mx:Script>

</mx:Application>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics