Subtract arrays
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Subtract arrays

  1. #1
    Junior Member
    Join Date
    May 2005
    Posts
    26

    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!

  2. #2
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    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.

  3. #3
    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

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,808
    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

  5. #5
    Senior Member
    Join Date
    May 2010
    Posts
    178
    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

  6. #6
    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
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com