Flex Array 过滤重复值

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				layout="absolute"
				creationComplete="init()"
				fontSize="12">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private var myArray:Array=new Array();
			private var filteredArr:Array=new Array();
			private var keys:Array=new Array();

			private function init():void
			{
				myArray.push({data: 1, label: "one"});
				myArray.push({data: 1, label: "one"});
				myArray.push({data: 1, label: "one"});
				myArray.push({data: 1, label: "one"});
				myArray.push({data: 2, label: "two"});
				myArray.push({data: 2, label: "two"});
				myArray.push({data: 2, label: "two"});
				myArray.push({data: 1, label: "one"});
				myArray.push({data: 3, label: "three"});
				myArray.push({data: 3, label: "three"});
				yuan.dataProvider=myArray;

//				myArray.push("wang");
//				myArray.push("wang");
//				myArray.push("yan");
//				myArray.push("wang");
//				myArray.push("xiaoxiaocainiao");
//				myArray.push("xiaoxiaocainiao");
//				myArray.push("xiaoxiaocainiao");
//				myArray.push("小小菜鸟");
//				myArray.push("小小菜鸟");
//				myArray.push("小小菜鸟");
//				yuan.dataProvider=myArray;
			}

			private function onclick(event:MouseEvent):void
			{
				keys=null;
				keys=new Array();
				filteredArr=myArray.filter(removedDuplicates);
				mubiao.dataProvider=filteredArr;
			}

			private function removedDuplicates(item:*, idx:uint, arr:Array):Boolean
			{
				if (keys.hasOwnProperty(item.data))
				{
					/* If the keys Object already has this property,
					 return false and discard this item. */
					return false;
				}
				else
				{
					/* Else the keys Object does *NOT* already have
					   this key, so add this item to the new data
					 provider. */
					keys[item.data]=item;
					return true;
				}

//				if (keys.indexOf(arr[idx]) != -1)
//				{
//					trace(arr[idx]);
//					trace(1);
//					return false;
//				}
//				else
//				{
//					trace(arr[idx]);
//					trace(-1);
//					keys.push(arr[idx]);
//					return true;
//				}
			}
		]]>
	</mx:Script>
	<mx:Button x="144"
			   y="285"
			   label="过滤"
			   click="onclick(event)"
			   fontSize="12"/>
	<mx:List id="yuan"
			 x="10"
			 y="47"
			 width="150"
			 height="200">
	</mx:List>
	<mx:List id="mubiao"
			 x="177"
			 y="47"
			 width="150"
			 height="200">
	</mx:List>
	<mx:Label x="51"
			  y="21"
			  text="源数组"
			  fontSize="12"/>
	<mx:Label x="189"
			  y="21"
			  text="目标数组"
			  fontSize="12"/>
</mx:Application>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值