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");
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");
1 Answer
Reset to default 21You 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'));
[col]
contained"hello"
in any of the[row
]? You add a flag in yourj
loop (assuming you usedi
for col andj
for row.) – Adelin Commented Jan 31, 2018 at 9:01