import flash.utils.Dictionary;
var a:Array = ["Tom", "John", "Susan", "Marie", "Tom", "John", "Tom", "Eva"];
var b:Dictionary = new Dictionary(true);//has map/cannot have duplicate keys
var c:Array = [];//filtered/unique entries array
for(var i:int = 0; i < a.length; i++) b[a[i]] = i;//hash array entries
for(var key:String in b) c.push(key);//put them back into an array
b = null;//clear/release the hash map
trace(c);//traces: Tom Eva John Susan Marie
var array:Array = [1,1,2,2,4,5];
trace(array);
trace(removeDuplicates(array));
function removeDuplicates(inArray:Array):Array
{
return inArray.filter(removeDuplicatesFilter);
}
function removeDuplicatesFilter(e:*, i:int, inArray:Array):Boolean
{
return (i == 0) ? true : inArray.lastIndexOf(e, i - 1) == -1;
}
此外,我建议您创建一个类,并将其实现为静态方法,类似于。
public class ArrayUtils
{
public static function removeDuplicates(inArray:Array):Array
{
return inArray.filter(_removeDuplicatesFilter);
}
protected static function _removeDuplicatesFilter(e:*, i:int, inArray:Array):Boolean
{
return (i == 0) ? true : inArray.lastIndexOf(e, i - 1) == -1;
}
}
也用像:
var array:Array = [1,1,2,2,4,5];
trace(ArrayUtils.removeDuplicates(array));
5条答案
按热度按时间y1aodyip1#
另一个选择是使用散列Map/Dictionary,如下所示:
vaj7vani2#
可以看出here
sh7euo9m3#
请尝试以下内容:
此外,我建议您创建一个类,并将其实现为静态方法,类似于。
也用像:
ghg1uchk4#
请使用Underscore.as!
https://github.com/amacdougall/underscore.as
avwztpqn5#