Is it possible to subtract array1 [1, 2, 3] from array2[1,2,3,4,5,6] and get array3[4,5,6]
Thanks for the help!
Printable View
Is it possible to subtract array1 [1, 2, 3] from array2[1,2,3,4,5,6] and get array3[4,5,6]
Thanks for the help!
Try something like this:
code:
arrA=[1,2,3];
arrB=[1,2,3,4,5,6];
function subtractArray(arr1,arr2){
for(var i in arr1){
var item=arr1[i];
for(var j in arr2){
if(arr2[i]==item){
arr2.splice(i,1);
}
}
}
trace(arr2);
}
subtractArray(arrA,arrB);
K.
Hi Deadbeat,
This code is working fine in AS2 but not in AS3. I cannot figure out how to solve it. Could you please tell me the solution.
Thanks,
Salim
I have no idea how it worked in AS2, but the variable i should've been replaced by variable j after the second for loop:
Actionscript Code:var arrA:Array = [1,2,3];
var arrB:Array = [1,2,3,4,5,6];
function subtractArray(arr1,arr2){
for(var i in arr1){
var item=arr1[i];
for(var j in arr2){
if(arr2[j]==item){
arr2.splice(j,1);
}
}
}
trace(arr2);
}
subtractArray(arrA,arrB);
Actionscript Code:var arrA:Array = [1, 12, 3, 8, 9, 10, 21];
var arrB:Array = [1, 2, 3, 4, 5, 6];
trace(arrA);
trace(arrB);
var arr:Array=isElement(arrA,arrB);
trace(arrA);
trace(arr)
trace(arrB);
function isElement(a:Array,b:Array):Array {
for (var i in a) {
for (var j in b) {
if (a[i]==b[j]) {
b.splice(j,1);
}
}
}
return b;
}
Function without dependency
poltuda
Thank you Nig 13 and Poltuda :)