subtract elements in arrays

Discussion in 'JavaScript' started by drawmedia, Dec 5, 2006.

  1. #1
    Hello. I need your help here..
    I have two arrays, like:

    array1: 1,2,3,4
    and
    array2: 2,4

    and I need to subtract values from the array1, that are present in the array2, so in the end i get array3: 1,3. I don't know the order of the values.. they can be like 5 3 1 2..

    Any ideas or tips on how I can do this?

    Thank you
     
    drawmedia, Dec 5, 2006 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You need a nested for loop to compare the elements of the two arrays. When a match is found, use
    Array.splice to remove the matching elements. Simple.
     
    Logic Ali, Dec 5, 2006 IP
  3. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    u have to check the array values in a for loop and set the array value to NULL.. whereever same values are there..
     
    weknowtheworld, Dec 23, 2006 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Doing that would produce 1,,3,
    Setting an element to null does not remove it from the array.
     
    Logic Ali, Dec 23, 2006 IP