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中的两个二维数组
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01