如何检查二维数组是否包含字符串? [英] How to check if a two dimensional array includes a string?

查看:79
本文介绍了如何检查二维数组是否包含字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组 arr [cols] [rows] .我想知道cols是否包含字符串"hello".如何使用 .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.

请注意,我正在尝试使用计数器 i 在循环中进行检查.所以我必须要做类似 arr [i] [0] .includes("hello");

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

推荐答案

您可以将 array.prototype.some array.prototype.includes 一起使用.应该是:

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

这篇关于如何检查二维数组是否包含字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆