-5

I have an array like this:

var winArray = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [7, 5, 3] ];

And another array like this: var Xarray = [1,2,3]

How can I check that the first array's index 0 contains my second array? Another issue is that the value order should not be regarded during the compression

George Sharvadze
  • 560
  • 9
  • 25
  • @BenjaminTrent: Not really. That won't work for arrays, as they won't be `==` to each other (unless they point to the same instance, which is unlikely). – Matt Aug 12 '14 at 19:04
  • Compare winArray[0][0] to Xarray[0], winArray[0][1] to Xarray[1] and winArray[0][2] to Xarray[2]. If they are all equal, they are the same. – Coda17 Aug 12 '14 at 19:05
  • @Coda17 That's not the solution. I don't always know the value of XArray and index of winArray... – George Sharvadze Aug 12 '14 at 19:07
  • Check this SO question and loop through the first deminsion of winArray: http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript – Benjamin Trent Aug 12 '14 at 19:10
  • @GeorgeSharvadze You specifically said "the first array's index 0 contains my second array" which is what I answered. – Coda17 Aug 12 '14 at 20:26

2 Answers2

1

Copy the arrays to new arrays, so that you can sort them, then compare each item in the arrays:

var a1 = winArray[0].slice(0);
var a2 = Xarray.slice(0);

var equal = a1.length == a2.length;
if (equal) {
    a1.sort();
    a2.sort();
    for (var i = 0; i < a1.length; i++) {
        if (a1[i] != a2[i]) {
            equal = false;
            break;
        }
    }
}

Demo: http://jsfiddle.net/Guffa/oa11qc4r/


If you mean that you want to search the array for a match, and not only compare the array at index 0, you would loop through the array and compare each array:

var a1 = Xarray.slice(0);
a1.sort();

var equal = false;
for (var j = 0; !equal && j < winArray.length; j++) {
    var a2 = winArray[j].slice(0);

    equal = a1.length == a2.length;
    var index = j;
    if (equal) {
        a2.sort();
        for (var i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i]) {
                equal = false;
                break;
            }
        }
    }
}

Demo: http://jsfiddle.net/Guffa/oa11qc4r/1/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • That's close. What if I don't know the index of the winArray? Your code will only work if the Xarray = winArray[0] – George Sharvadze Aug 12 '14 at 19:21
  • @GeorgeSharvadze: I figured that you might mean that, even if you specifically asked for how to check the array at index 0. I already added code for that. – Guffa Aug 12 '14 at 19:24
0

Might give this a shot. The function will return the index of the 'child array' if its found in the main data array. If its not found -1 will be returned.

var winArray = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [1, 4, 7],
    [2, 5, 8],
    [3, 6, 9],
    [1, 5, 9],
    [7, 5, 3]
];

var Xarray = [1,2,3];

function indexOfPredicate(data, value, predicate) {
  if (typeof(predicate) !== 'function') {
    throw "Predicate must be a function"
  }

  for(var x = 0; x < data.length; x++) {
    if (predicate(data[x], value)) {
        return x
    }
  }

  return -1;
}

function indexOfMulti(data, value) {
  return indexOfPredicate(data, value, function(a, b) {
    if (a.length !== b.length) {
      return false;
    }

    for(var i = 0; i < a.length; i++) {
      if (a[i] !== b[i]) {
        return false;
      }
    }

    return true;
  })
}

var index = indexOfMulti(winArray, Xarray);
var contains = index > -1;

console.log(index);
console.log(contains);
klyd
  • 3,939
  • 3
  • 24
  • 34