需要将变量与数组进行比较

时间:2014-04-05 20:21:29

标签: javascript arrays

我有这两个数组;

theImages1 = new Array();
    theImages1[0] = 'images/1-0000-0400-0487-3.6-0.9.gif'
    theImages1[1] = 'images/1-0000-0450-0526-3.2-0.8.gif'
    theImages1[2] = 'images/1-0000-0500-0568-2.9-0.7.gif'
theImages2 = new Array();
    theImages2[0] = ['images/1-0000-0400-0487-3.6-0.9.gif', 100000.5]
    theImages2[1] = ['images/1-0000-0450-0526-3.2-0.8.gif', 200000.6]
    theImages2[2] = ['images/1-0000-0500-0568-2.9-0.7.gif', 300000.7]

theImages1在计时器上随机显示为变量imageDisplay。

我需要将imageDisplay与images2进行比较,如果它们匹配,我需要检索图像右侧长数字的第二部分的值

示例300000.7

我一直在尝试使用循环和if语句完成比较,如下所示......

canoe = imageDisplay
for (i=0; i<theImages11.length; i++) {
    if (canoe == theImages11[i]) {
        alert("yes")
    }
    else {
        alert("no")
    }
}

但是,由于Images2数组的第二部分,警报始终是&#34; no&#34;。我也不知道如何检索数组的第二部分,即数字。 感谢您的任何反馈。

1 个答案:

答案 0 :(得分:0)

鉴于您的初始数据:

theImages1 = new Array();
theImages1[0] = 'images/1-0000-0400-0487-3.6-0.9.gif'
theImages1[1] = 'images/1-0000-0450-0526-3.2-0.8.gif'
theImages1[2] = 'images/1-0000-0500-0568-2.9-0.7.gif'

theImages2 = new Array();
theImages2[0] = ['images/1-0000-0400-0487-3.6-0.9.gif', 100000.5]
theImages2[1] = ['images/1-0000-0450-0526-3.2-0.8.gif', 200000.6]
theImages2[2] = ['images/1-0000-0500-0568-2.9-0.7.gif', 300000.7]

您只需要更好地了解正在使用的数组索引:

canoe = imageDisplay // e.g., images/1-0000-0500-0568-2.9-0.7.gif

for (i=0; i<theImages2.length; i++) {
    if (canoe == theImages2[i][0]) // first index pos of each entry
        return theImages2[i][1]; // return the 2nd index pos (the value)
    }
}
return false; // no match, return false or whatever other failure
相关问题