comparing two 2d arrays in jquery(比较jquery中的两个二维数组)
问题描述
我正在尝试比较两个二维数组,我只需要它们完全相同时才匹配.我的代码太长了,因为数组可能会更长.我尝试使用 .each() 和 for 循环,但它变得非常混乱,不会比较每个数组.
I'm trying to compare two arrays both 2d, I need it only match when they're completely identical. The code I have is too lengthly, as the arrays will potentially be far longer. I tried playing with .each() and for loops but it get's very messy and won't compare every array.
var solution=[
[0,0,0],
[0,0,1],
[0,0,1]];
var value=[
[0,0,0],
[0,0,1],
[0,0,1]];
//compare arrays
if (solution[0][0]==value[0][0] &&
solution[0][1]==value[0][1] &&
solution[0][2]==value[0][2] &&
solution[1][0]==value[1][0] &&
solution[1][1]==value[1][1] &&
solution[1][2]==value[1][2] &&
solution[2][0]==value[2][0] &&
solution[2][1]==value[2][1] &&
solution[2][2]==value[2][2]) {
$('h1').show();
}
else { $('h1').hide();}
推荐答案
简单的技巧,把它们变成字符串 :)
simple trick, by making them into strings :)
function equalArray(a, b) {
return JSON.stringify(a) == JSON.stringify(b);
}
这篇关于比较jquery中的两个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较jquery中的两个二维数组


基础教程推荐
- npm start 错误与 create-react-app 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01