Please Help with this array

Discussion in 'JavaScript' started by JEET, Oct 23, 2008.

  1. #1
    Hi,
    I have 2 arrays

    ww= (401, 402, 403, 404);
    cc= (399, 400, 401);

    I need to find if there's a matching element in both arrays. Ex: 401 is matching above.

    I can do this by looping in both arrays, but that's slowing the script a lot. (this is inside another big loop)
    Is there a fast way of checking this?

    Kindly help
    Thanks :)
     
    JEET, Oct 23, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, I don't think you can do it without looping, anyway... You can search for 'javascript array_diff' if you want ready made function.
    Don't know about speed, but other option would be to convert each array into object, where the value of array will become the member of object, but that doesn't accept numbers, so you would need to add some non-decimal char e.g. '_401':0. Then you can use IN operator.
    Hard to say what's better, as I don't know much about the context...
     
    lp1051, Oct 23, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm pretty sure you can only do this by looping but it may be possible to change your logic so that you use a different method.

    What exactly are you doing?
     
    MMJ, Oct 23, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Hi,
    I am trying to find the coinciding points of 2 boxes (rectangles)
    These are 2 divs on the page and I need to know if they are "overlapping" each other (even touching) or not. If they are, the script shows an alert() message.
    Any way to detect this quickly will be best. Loops are slowing the script too much.
    I know the points that make the borders of these 2 divs... However if I could only use the four coordinates to do the check, it'll speed things a lot (not sure about accuracy).

    Thanks :)
     
    JEET, Oct 24, 2008 IP
  5. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hope this help
    <script>
    //x11, x12 - x coordinates, y11, y12 - y coordinates for first div
    //x21, x22 - x coordinates, y21, y22 - y coordinates for second div

    function overlapping(x11, x12, x21, x22)
    {
    return Math.max(x11, x21) < Math.min(x12, x22);
    }

    function rectanglesOverlap(x11, x12, x21, x22, y11, y12, y21, y22)
    {
    return overlapping(x11, x12, x21, x22)
    && overlapping(y11, y12, y21, y22);
    }

    x11 = 120;
    x12 = 220;

    x21 = 150;
    x22 = 210;

    y11 = 340;
    y12 = 450;

    y21 = 120;
    y22 = 420;


    alert(rectanglesOverlap(x11, x12, x21, x22, y11, y12, y21, y22));

    </script>
     
    cont911, Oct 24, 2008 IP
    JEET likes this.
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    Hi,
    Thanks for these functions. I'll try these. Green points added. :)
     
    JEET, Oct 25, 2008 IP