I have a two dimensional array arr[cols][rows].
I want to know if the cols contains a string "hello".
How can I check that using .includes("hello") method.
Please note that I am trying to check this inside a loop with counter i. So I have to do something like arr[i][0].includes("hello");
An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array. int x = a[1,1]; Console.
You can use array.prototype.some along with array.prototype.includes. It shoud be:
var datas= [
  ["aaa", "bbb"],
  ["ddd", "eee"]
];
function exists(arr, search) {
    return arr.some(row => row.includes(search));
}
console.log(exists(datas, 'ddd'));
console.log(exists(datas, 'xxx'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With