4

I have two multidimensional arrays:

first is something like (['one','one','three'],['four','five',five'],['one','one','one'])

and the second one is like this (['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']...)

Now, what I want is to find match first index of first array with second array, but position of at least first two indexes from boths array must match also, eg.:

first_array (['one','one','three'],['four','five',five'],['one','one','one'])

will match

second_array (['one','one','nine'],['one','one','one'],['two','two']['two','two','two']...)

and output would be eg. 'alert('Match.').

I have tried

for(i=0; i<1; i++){
    if(first_array[0] == second_array) console.log('Match');
    else console.log('No match');
}

but I constantly get 'No match' although there is a match. P.S. in 'for' loop, my i is i<1 because I want to compare only first index of first_array with complete second_array.

Thanks in advance

Kelvin
  • 1,004
  • 1
  • 10
  • 32
mihajlo
  • 108
  • 2
  • 4
  • 15

3 Answers3

8
var md1 = [['one','one','three'],['four','five','five'],['one','one','one']];

var md2 = [['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']];

//Iterate through all elements in first array
for(var x = 0; x < md1.length; x++){

    //Iterate through all elements in second array    
    for(var y = 0; y < md2.length; y++){

      /*This causes us to compare all elements 
         in first array to each element in second array
        Since md1[x] stays fixed while md2[y] iterates through second array.
         We compare the first two indexes of each array in conditional
      */
      if(md1[x][0] == md2[y][0] && md1[x][1] == md2[y][1]){
        alert("match found");
        alert("Array 1 element with index " + x + " matches Array 2 element with index " + y);
      }
    }
}

Working Example http://jsfiddle.net/2nxBb/1/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • will give it a shot, be back here in 5 minutes – mihajlo May 08 '13 at 09:49
  • @mihajloWR I just noticed a bug or two please use the updated. Mainly I was using md1[y] when I should have been using md1[x] and md2[y] – Kevin Bowersox May 08 '13 at 09:54
  • seems to be working..got to adjust it tomy needs but globally - it's working. Do you have spare time to explain the logic maybe, so I can fully understand it? – mihajlo May 08 '13 at 09:54
  • @mihajloWR I added comments to help explain whats occuring, basically its iterating through the second array for each element in the first array and doing a comparison – Kevin Bowersox May 08 '13 at 09:58
  • The first version works as I wanted, but the new updated don't, just for you to know :). Thank you for explanations and your time, you help me a lot. – mihajlo May 08 '13 at 10:01
  • Beware this solution will only work if you're certain the arrays will contain strings or numbers. See my solution for arrays thay may contain arrays. – Robbert May 08 '13 at 10:14
3

Possible duplicate of How to compare arrays in JavaScript?.

For a strict array comparison, check their length and values like so:

var a1 = [1, 2, 3];
var a2 = [1, 2, 3];

array_compare(a1, a2);

function array_compare(a1, a2) {
 if(a1.length != a2.length) {
  return false;
 }
 for(var i in a1) {
  // Don't forget to check for arrays in our arrays.
  if(a1[i] instanceof Array && a2[i] instanceof Array) {
   if(!array_compare(a1[i], a2[i])) {
    return false;
   }
  }
  else if(a1[i] != a2[i]) {
   return false;
  }
 }
 return true;
}
Community
  • 1
  • 1
Robbert
  • 5,063
  • 6
  • 36
  • 44
1

2 way, more simple if this enough for u

JSON.stringify(tree1) === JSON.stringify(tree2)

if not, use this: recursively handles multidimensional arrays and objects

treesAreSame(tree1, tree2) {
        if (tree1 === tree2) {
            return true;
        }
        if (tree1 === null || tree1 === undefined || tree2 == null) {
            return false;
        }
        if (Array.isArray(tree1) !== Array.isArray(tree2)) {
            return false;
        }
        if (tree1.length !== tree2.length) {
            return false;
        }
        if (isArray(tree1)) {
            for (let i = 0; i < tree1.length; ++i) {
                let t1 = tree1[i];
                let t2 = tree2[i];
                if (isArray(t1) || isObject(t1)) {
                    if (!treesAreSame(t1, t2)) {
                        return false;
                    }
                } else {
                    if (t1 !== t2) {
                        return false;
                    }
                }
            }
        }
        if (isObject(tree1)) {
            for (const k of Object.keys(tree1)) {
                let t1 = tree1[k];
                let t2 = tree2[k];
                if (isArray(t1) || isObject(t1)) {
                    if (!treesAreSame(t1, t2)) {
                        return false;
                    }
                } else {
                    if (t1 !== t2) {
                        return false;
                    }
                }
            }
        }
        return true;
    };
    isObject(a) {
        return (!!a) && (a.constructor === Object);
    };
    isArray(a) {
        return (!!a) && (a.constructor === Array);
    };
WMage
  • 11
  • 1