-
Subtract arrays
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.
-
Senior Member
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
Smile is a small curve which can solve big problems
-
Prid - Outing
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);
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
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
-
Senior Member
Thank you Nig 13 and Poltuda
Smile is a small curve which can solve big problems
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|