比较jQuery中2二维数组 [英] comparing two 2d arrays in jquery

查看:287
本文介绍了比较jQuery中2二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个数组二维的,我需要它,只有当他们是完全一致的匹配。在code我有过过长的,因为阵列将可能成为更长的时间。我试着用。每()和循环播放,但它得到的非常混乱,也不会每个数组进行比较。

  VAR解决方案= [
[0,0,0]
[0,0,1]
[0,0,1]];VAR值= [
[0,0,0]
[0,0,1]
[0,0,1]];//比较数组
    如果(溶液[0] [0] ==值[0] [0]&放大器;&放大器;
        溶液[0] [1] ==值[0] [1]&放大器;&放大器;
        溶液[0] [2] ==值[0] [2]&放大器;&放大器;
        溶液[1] [0] ==值[1] [0]&放大器;&放大器;
        溶液[1] [1] ==值[1] [1]&放大器;&放大器;
        溶液[1] [2] ==值[1] [2]&放大器;&放大器;
        溶液[2] [0] ==值[2] [0]&放大器;&放大器;
        溶液[2] [1] ==值[2] [1]&放大器;&放大器;
        溶液[2] [2] ==值[2] [2]){        $(H1)显示()。    }
    其他{$(H1),隐藏();}


解决方案

简单的技巧,使它们成字符串:)

 函数equalArray(A,B){
    返回JSON.stringify(一)== JSON.stringify(二);
}

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中2二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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