0

I have a 2D array and two 1D arrays. I have to compare the 1D array with the 2D array in JavaScript. I have written the following code but it is not working. Any help would be appreciated.

<!DOCTYPE html>
<html>
<body>

<script>

  var arr = [ 
    ['Cat', 'Brown', 2],
    ['Parrot', 'Brown', 1]
  ];
  
  var col = [0,1];
  var key = ['Parrot','Brown'];
 
  for (var i = 0; i < arr.length; i++){
    for (var j = 0; j < col.length; j++){
        var isMatched = arr[i][col[j]] == key[j];
        if (isMatched){
            // write arr index
            document.write(i); 
            // it should write 1 but writing 001
        }    
    }  
  }
 
</script>

</body>
</html>

I am using 'col' array to store index of columns to be compared like in this case 0 for first column with values cat, parrot and so on. What I need is if all elements of 'key' array matches with any array of 'arr' then it should print true and index of matching array in 'arr'

  • On what basis are you comparing? Say for the first column with cat and parrot, and you're comparing it against parrot: should it return true/false? What is the expect output? – Terry Jun 17 '21 at 13:44
  • Do `document.write(i + '
    ');` you'll see what is happening.
    – Teemu Jun 17 '21 at 13:46
  • I can't seem to understand what're you trying to achieve here. Could you elaborate more on your question? Why are you using a col array with [0,1] and how do you want to compare the elements of those two arrays? – ishandeveloper Jun 17 '21 at 13:46
  • @Terry if all elements of 'key' array matches with any array of 'arr' then it should print true and index of matching array in 'arr'. – Muhammad Tayyab Jun 17 '21 at 13:51
  • is @ishandeveloper mentioned, this question requires more clarification. Could you add wanted results as example? – user3133 Jun 17 '21 at 13:51
  • @ishandeveloper I am using 'col' array to store index of columns to be compared like in this case 0 for first column with values cat, parrot and so on. What I need is if all elements of 'key' array matches with any array of 'arr' then it should print true and index of matching array in 'arr' – Muhammad Tayyab Jun 17 '21 at 13:54
  • @user3133 document.write(i); should write 1 (index of arr) to screen instead of 001 which it currently writes with above code. – Muhammad Tayyab Jun 17 '21 at 13:58
  • It doesn't write "001", it writes a single number (the value of `i`) every time the condition passes in the loop. – Teemu Jun 17 '21 at 14:23

2 Answers2

0

Based on what you've mentioned in the comments, if you just want to check whether all the values in key array are contained in the other list of arrays arr. Then you don't necessarily need a col array and the following script should work.

var arr = [
  ["Cat", "Brown", 2],
  ["Parrot", "Brown", 1],
];

var key = ["Parrot", "Brown"];

for (let i = 0; i < arr.length; i++) {

  // Current array from array of arrays, which you want to search  
  let array_to_be_searched = arr[i];

  // Let's initially assume that all the elements of the key are included in this one
  let isMatched = true;

  for (let j = 0; j < key.length; j++) {
    if (!array_to_be_searched.includes(key[j])) {
      isMatched = false;
      break;
    }
  }

  // If our assumption is correct
  if (isMatched) {
    document.write(true); // Write true :)
    document.write(i); // Index of current array
  }
}

ishandeveloper
  • 176
  • 1
  • 7
0

The output you are getting is "011" and not "001" are the index of "parrot" in key and then two time the index of "brown" in key. After reading you comment I understood that you meant that "parrot" and "brown" should be found as a set in on of arr sub arrays.

So you have made the wrong comparisons - check this answerer to do the right one - Check if an array contains any element of another array in JavaScript

and them make one loop to make the comparison.

Dharman
  • 30,962
  • 25
  • 85
  • 135