Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  163] [ 2]  / answers: 1 / hits: 22508  / 6 Years ago, wed, january 31, 2018, 12:00:00

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);


More From » arrays

 Answers
38

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'));




[#55306] Sunday, January 28, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
koltenb

Total Points: 276
Total Questions: 92
Total Answers: 101

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;