-2

I'm working with an array of arrays that looks like this:

let matrix = [[0,1,1,2], [0,5,0,0], [2,0,3,3]]

I want to loop through each array and compare the value to the same element in the other arrays i.e. I want to compare the 0th element in the 0th array to the 0th elements in the 1th and 2nd array (in this case I would be comparing 0 to 0 and 2 respectively).

I want to iterate through the arrays comparing the current array element in the current array to it's counterpart in the following arrays i.e. look at the 0th element of this array and compare it to the 0th element in the next two arrays, then look at the 1th element in this array and compare it to the 1th element in the next two arrays, and so forth.

Compare the values 0, 0, 2
Then compare 1,5,0
Then compare 1,0,3,
Then compare 2, 0, 3

How can I do this with nested for loops? Is there a better way to do this not involving nested for loops?

Nick Kinlen
  • 1,356
  • 4
  • 30
  • 58
  • Possible duplicate of [JavaScript - Compare two multidimensional arrays](https://stackoverflow.com/questions/16437307/javascript-compare-two-multidimensional-arrays) – Tibike Oct 25 '19 at 22:23
  • 1
    @Tibike This isn't about comparing two arrays, it's about comparing rows in the same array. – Barmar Oct 25 '19 at 22:28
  • What do you mean by comparing all the elements? You want to print whether they're all the same? – Barmar Oct 25 '19 at 22:32
  • This is the second time I asked a question about comparing elements in the SAME nested array (but in different arrays within the parent array) and the second time I get downvoted and referred to a post about comparing two different nested arrays. Very frustrating. – Nick Kinlen Oct 25 '19 at 23:00

1 Answers1

2

Using 2 for loops:

let matrix = [[0,1,1,2], [0,5,0,0], [2,0,3,3]];

if (matrix.length > 0 && matrix.every(arr => arr.length === matrix[0].length)) {
  // Loop on the length of the first Array
  for (let i = 0; i < matrix[0].length; i++) {
    const valuesToCompare = [];
    // Loop on each Array to get the element at the same index
    for (let j = 0; j < matrix.length; j++) {
      valuesToCompare.push(matrix[j][i]);
    }
    console.log(`Comparing ${valuesToCompare}`);
  }
} else {
    console.log(`The matrix must have at least one Array,
                 and all of them should be the same length`);
}

Using forEach and map (still two loops, but less verbose):

let matrix = [[0,1,1,2], [0,5,0,0], [2,0,3,3]];

if (matrix.length > 0 && matrix.every(arr => arr.length === matrix[0].length)) {
  // Loop on the length of the first Array
  matrix[0].forEach((_, i) => {
    // Map each Array to their value at index i
    const valuesToCompare = matrix.map(arr => arr[i]);
    console.log(`Comparing ${valuesToCompare}`);
  });
} else {
    console.log(`The matrix must have at least one Array,
                 and all of them should be the same length`);
}
blex
  • 24,941
  • 5
  • 39
  • 72
  • YOu're not actually comparing anything. – Barmar Oct 25 '19 at 22:29
  • 2
    The OP has not said how they should be compared and what the output should be. I chose not to guess, and just answer the question, which is about loops. – blex Oct 25 '19 at 22:30
  • The loops worked for me. Earlier I had a very similar approach but it turns out I had the `matrix[j][i]` part backwards and was doing `matrix[i][j]` and that was the cause of my undefined error. Thanks for the help. – Nick Kinlen Oct 26 '19 at 05:17