javascript - 如何检查该数组是否包含(数字)数组?
问题描述
我将几个坐标对存储在一个数组中。例如:
[[1,2],[4,5],[7,8]]
我想找到我得到的坐标对。我得到了坐标:
[4,5]
如果数组包含这个坐标,我会得到真否则假。这是我的代码:
function isContain(coords) {
for (let i = 0; i < array.length; i++) {
if (array[i].length !== coords.length) return false;
for (let j = 0; j < array[i].length; j++) {
if(array[i][j] === coords[j]) {
return true
}
}
}
return false
}
解决方案
你可以做这样的事情
const foo = [[1, 2], [4, 5], [7, 8]];
const coords = [4, 5];
function isContain(coords) {
for (const f of foo) {
if (f[0] === coords[0] && f[1] === coords[1]) {
return true;
}
}
return false;
}
console.log(isContain(coords));
推荐阅读
- android - 我实现了 VirtualAPK 库并让任务 ':app:compileDebugJavaWithJavac' 的执行失败
- javascript - 使用 PHP (in_array) 检查值是否存在于数组中
- python - python程序中的Prolog查询未返回正确答案
- mysql - 我需要帮助加入两个查询以获得一个表作为结果
- c# - 即使服务器端允许本地地址,CORS 也会阻止访问
- c# - 为什么 IP 地址返回值 127.0.0.1?
- ios - UIImage 在屏幕/窗口/超级视图上的位置
- javascript - 关闭总是需要在 IIFE 中吗?
- php - 使用 HTTP POST REQUEST 将数据从 ESP8266 发送到 Live Server 的问题
- android - 如何忽略 NFC 意图(使手机不振动)?