Rey Something
Rey Something

Reputation: 21

Loop in multidimensional array not showing what is expected

Hello I try trying to compare some values in a multidimensional array but when I try to console.log don't show anything I think is correct but not showing. Did I did something wrong?.

var arr = [
    [['cat', 'fish'],['dog', 'meat']],
    [['cat', 'toy'],['dog', 'bone']],
    [['cat', 'fish'],['dog', 'bone']]
];

var position = 0;
for(var i =0; i < arr.length;i++){
  for(var j = 0; j < arr[i].length;j++){
    for(var g = 0; g < arr[i].length;g++){
      if(j != g && arr[i][j][0] === arr[i][g][0]){
        console.log(arr[i][j][1]);
      }
    }
  }
}
//expecting fish, bone;

The problem is in the if statement but I can't see why.

Upvotes: 0

Views: 53

Answers (2)

Makyen
Makyen

Reputation: 33356

What you are attempting to do is not clear. The if statement that you are using will always compare 'cat' to 'dog' which will result in no output.

Below is your code, modified to show what you are comparing. In general, adding additional logging output is a reasonable way to debug by providing information as to what is really happening.

Note: I changed the start position for the g loop to j + 1. You will have already compared all entries in a prior run of the loop for those where g < j and your don't want to compare things against themselves. Obviously, this means that your if statement no longer needs to check for j == g.

Your comparisons are:

cat === dog is false
cat === dog is false
cat === dog is false

Given that all of the conditions you evaluate in your if statement are false, you don't get any output from your console.log(arr[i][j][1]);

var arr = [
    [['cat', 'fish'],['dog', 'meat']],
    [['cat', 'toy'],['dog', 'bone']],
    [['cat', 'fish'],['dog', 'bone']]
];

var position = 0;
for(var i =0; i < arr.length;i++){
  for(var j = 0; j < arr[i].length;j++){
    //You will have already compared all entries with g<j and we don't
    //  want to compare against itself.
    for(var g = j + 1; g < arr[i].length;g++){
      console.log(arr[i][j][0] + ' === ' + arr[i][g][0] 
                  + ' ([' + i + '][' + j + '][0] === [' + i + '][' + g + '][0])'
                  + " is " + (arr[i][j][0] === arr[i][g][0]));
      if(arr[i][j][0] === arr[i][g][0]){
        console.log('Match: ' + arr[i][j][1]);
      }
    }
  }
}

However, based on the data you have, what you might be attempting to do is compare the kind of thing the animal is playing with/eating. If this is the case, the issue may be that you are confusing which index is for which portion of the multidimensional array.

var arr = [
    //         [a][0][c]                     [a][1][c]
    //[a][0][0]         [a][0][1]   [a][0][0]         [a][0][1]
    [[  'cat',            'fish' ],[  'dog',            'meat' ]],  // [0][b][c]
    [[  'cat',            'toy'  ],[  'dog',            'bone' ]],  // [1][b][c]
    [[  'cat',            'fish' ],[  'dog',            'bone' ]]   // [2][b][c]
];

var position = 0;
for(var i =0; i < arr.length;i++){
  for(var iP = i + 1; iP < arr.length;iP++){
    for(var j = 0; j < arr[i].length;j++){
      console.log(arr[i][j][1] + ' === ' + arr[iP][j][1] 
                  + ' ([' + i + '][' + j + '][1] === [' + iP + '][' + j + '][1])'
                  + " is " + (arr[i][j][1] === arr[iP][j][1]));
      if(arr[i][j][1] === arr[iP][j][1]){
        console.log('Match: ' + arr[i][j][1]);
      }
    }
  }
}

Upvotes: 1

Bob__
Bob__

Reputation: 12779

If I understood your intent, you have to change your loops and the condition:

var arr = [
    [['cat', 'fish'],['dog', 'meat']],
    [['cat', 'toy'],['dog', 'bone']],
    [['cat', 'fish'],['dog', 'bone']]
];

for (var i = 0; i < arr.length; i++){
  // compare only different rows
  for (var k = i + 1; k < arr.length; k++) {
    // inside each row, compare the columns elements
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j][0] === arr[k][j][0] && arr[i][j][1] === arr[k][j][1]) {
         //  ^^^ compare      ^^^ different   ^^^  rows        ^^^
         console.log(arr[i][j][1]);
      }
    }
  }
}

Upvotes: 0

Related Questions