Arrays Flex 3 Actionscript数组减法函数
有人能告诉我如何比较两个数组并删除ActionScript中的常用术语吗 例如: 如果使用,则很容易进行如下集合数学:Arrays Flex 3 Actionscript数组减法函数,arrays,actionscript,subtraction,Arrays,Actionscript,Subtraction,有人能告诉我如何比较两个数组并删除ActionScript中的常用术语吗 例如: 如果使用,则很容易进行如下集合数学: var array1:Array = [2,4,6,8,10,12]; var array2:Array = [1,2,3,4,5,6,7,8,9,10,11]; var subtraction:Array = Enumerable.from(array1) .except(Enumerable.from(array2))
var array1:Array = [2,4,6,8,10,12];
var array2:Array = [1,2,3,4,5,6,7,8,9,10,11];
var subtraction:Array = Enumerable.from(array1)
.except(Enumerable.from(array2))
.toArray();
您可以使用自定义函数进行筛选。 这不是一种过滤阵列差异的优化方法,但它可以完成这项工作
subtraction = Array1.filter(function(item:*, index:int, arr:Array){
var i:int;
var l:int;
l = Array2.length;
for ( i=0; i < l; i++ )
{
if ( Array2[i] == item )
{
return false;
}
}
return true;
});
减法=数组1.过滤器(函数(项:*,索引:int,数组){
变量i:int;
变量l:int;
l=阵列2.长度;
对于(i=0;i
如果您希望从数组中删除所有重复项,我建议您使用集合以尽可能快地提高查找速度:
const a : Array = [ 2, 3, 4 ];
const b : Array = [ 3, 4, 5 ];
// Create a Set for Array 'b' to provide a fast lookup table.
const setB : Object = {};
var prop : *;
for each (prop in b) {
setB[key] = true
};
// Find all values which only belong in Array 'a'.
const uniqueToA : Array = [];
for each (prop in a) {
if (setB[prop] === undefined) {
uniqueToA.push(prop);
}
}
如果您发现自己在收集方面做了很多工作,那么我建议您投资收集框架,例如。是的,对不起;当我发布这条评论时,布莱恩并没有做好准备——正是“删除常用术语”这一点让我抓狂。
const a : Array = [ 2, 3, 4 ];
const b : Array = [ 3, 4, 5 ];
// Create a Set for Array 'b' to provide a fast lookup table.
const setB : Object = {};
var prop : *;
for each (prop in b) {
setB[key] = true
};
// Find all values which only belong in Array 'a'.
const uniqueToA : Array = [];
for each (prop in a) {
if (setB[prop] === undefined) {
uniqueToA.push(prop);
}
}