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'
');` you'll see what is happening. – Teemu Jun 17 '21 at 13:46