How to remove Duplicates from an Array

本文介绍了一种在Flex中去除数组重复元素的方法。通过排序和遍历数组,该方法可以有效地保留唯一值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flex : How to remove Duplicates from an Array

Tagged Under : Flex

If you have an Array and you want to get rid of duplicates from the array, here is a little function you can use to achieve the same.

  1. private function removeDuplicates(arr:Array):Array  
  2. {  
  3. var currentValue:String = "";  
  4. var tempArray:Array = new Array();  
  5. arr.sort(Array.CASEINSENSITIVE);  
  6. arr.forEach(  
  7. function(item:*, index:uint, array:Array):void {  
  8. if (currentValue != item) {  
  9. tempArray.push(item);  
  10. currentValueitem;  
  11. }  
  12. }  
  13. );  
  14. return tempArray.sort(Array.CASEINSENSITIVE);  
  15. }  

Basically, all its doing is making use of the forEach function built in Flex that runs a custom function for every item in the Array. This function then sorts the data alphabetically, checks each value with the previous value and if they are not same, adds the unique element to the new tempArray and thats what gets returned.

Might be useful for some I guess!

 

var foo = new Array("1", "2", "3", "4", "5", "6");
foo.splice(4);
//foo is now 1, 2, 3, 4
foo.splice(1,1);
//foo is now 1, 3, 4
foo.splice(1, 0, "blah")
//used splice to insert "blah" into foo.
//foo is now 1, blah, 3, 4
//Note the use of zero for the deleteCount
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值