我正在try 让if语句检测这两个数组是否有相等的部分.

const winningCombos = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
const mySpots = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

function test() {
  winningCombos[0].splice(0, 3, 'testsss')
  //position 0 remove 1
  if (winningCombos[0] || winningCombos[1] || winningCombos[2] === mySpots[0] || mySpots[1] || mySpots[2]) {
    document.getElementById("test").innerHTML = 'sucess'
  } else {
    document.getElementById("test").innerHTML = 'fail'
  }
}

但我也想让if语句变成

if ((winningCombos[0] OR winningCombos[1] OR winningCombos[2]) is equal to(mySpots[0] || mySpots[1] || mySpots[2]) {
    console.log('success');
  };

有什么你们都能找到的解决方案吗?

推荐答案

您必须迭代数组并比较项

/**
 * Compares each row of two arrays to determine if they are equal.
 * Rows are considered equal if they have the same length and all corresponding elements are equal.
 * 
 * @param {Array<Array<number>>} a - The first array of rows to compare.
 * @param {Array<Array<number>>} b - The second array of rows to compare.
 * @returns {Array<boolean>} An array of boolean values, each representing whether the corresponding rows in the arrays are equal.
 */
function findSimilarRows(a, b) {
  return a.map((rowA, index) => {
    const rowB = b[index];
    if (!rowB || rowA.length !== rowB.length) {
      return false;
    }

    return rowA.every((element, elementIndex) => element === rowB[elementIndex]);
  });
}

const a = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [1, 2, 3],
  [5, 9, 0]
];
const b = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [1, 2],
  [1, 2]
];

console.log(findSimilarRows(a, b));

Javascript相关问答推荐

fetch在本地设置相同来源的cookie,但部署时相同的代码不会设置cookie

react/redux中的formData在expressjs中返回未定义的req.params.id

如何在Angular中插入动态组件

Msgraph用户邀请自定义邮箱模板

如何使覆盖div与可水平滚动的父div相关?

MathJax可以导入本地HTML文档使用的JS文件吗?

如何添加绘图条形图图例单击角形事件

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

JQuery Click事件不适用于动态创建的按钮

Web Crypto API解密失败,RSA-OAEP

确保函数签名中的类型安全:匹配值

如何在Bootstrap中减少网格系统中单个div的宽度

如何找到带有特定文本和测试ID的div?

JavaScript -复制到剪贴板在Windows计算机上无效

如何在TransformControls模式下只保留箭头进行翻译?

正则表达式以确定给定文本是否不只包含邮箱字符串

无法向甜甜圈图表上的ChartJSImage添加可见标签

如何使用useparams从react路由中提取id

在执行console.log(new X())时记录一个字符串

此上下文在JavaScript中