Javascript jQuery.InArray()返回-1时出现问题

Javascript jQuery.InArray()返回-1时出现问题,javascript,jquery,arrays,Javascript,Jquery,Arrays,当我运行函数时,jQuery.InArray()返回-1时,我遇到了一个问题,但我知道我测试的值肯定在数组中 我知道它们在数组中,因为如果我在运行jQuery.inArray()之前立即使用console.log记录数组的值,这些值就会存在并显示出来。以下是我正在使用的示例: /* -- In page.php -- */ images = [8, 13, 21, 32, 40, 56]; /* -- In script.js -- */ console.log('images->'

当我运行函数时,
jQuery.InArray()
返回-1时,我遇到了一个问题,但我知道我测试的值肯定在数组中

我知道它们在数组中,因为如果我在运行
jQuery.inArray()
之前立即使用console.log记录数组的值,这些值就会存在并显示出来。以下是我正在使用的示例:

/* -- In page.php -- */
images = [8, 13, 21, 32, 40, 56]; 


/* -- In script.js -- */
console.log('images->', images); 
//output  images-> [8, 13, 21, 32, 40, 56]

var testingValue = 13;

console.log('inArray->', jQuery.inArray(testingValue, images)); //Should return 1
//output inArray-> -1

console.log('inArray->', jQuery.inArray(13, images)); //Should return 1
//output inArray-> -1

console.log('inArray->', jQuery.inArray("13", images)); //Should return -1
//output inArray-> -1

console.log('inArray->', jQuery.inArray('13', images)); //Should return -1 
//output inArray-> -1
所有这些都返回
-1
,即使前两个应该返回
1
。奇怪的是,如果我在控制台中手动测试
jQuery.inArray(testingValue,images)
,我会得到值1

需要注意的是:变量
images
是在包含js文件的php页面中创建的。这不应该是个问题

附加说明:是的,jQuery在这部分脚本运行之前就包含了。在此之前,许多其他jQuery函数都已成功运行


我想知道的是,这个函数是否有任何缺陷。我在过去成功地使用过它,在代码中看不到任何东西会阻止它返回正确的值。

经过一个晚上的良好休息后,我能够循环起来,重新审视这个问题。变量
testingValue=13显式转换为整数。问题是,我是通过jQuery
$动态获取该值的。每个(arrayName,function(index,value){})
。尽管数据显示
索引
确实是一个整数,但出于某种原因,
$。inArray()
没有将其视为int/float,因此在数值数组中找不到值。在检查变量是否正确之前,我将代码修改为
parseInt()
index
变量

长话短说 如果尝试将jQuery.inArray()用于整数或浮点数组时遇到问题,请分别尝试使用
parseInt()
parseFloat()
,以确保函数接收到正确的数据类型

if( jQuery.inArray(parseInt(yourVariable), yourArray) > -1)
{
    return true;
}

他们似乎知道它应该工作,只是因为我的代码中的某些原因它不工作。是否有任何东西会阻碍数组声明?奇怪的是,我可以做一个console.log,并明确地看到函数是用值定义的,只是我没有收到正确的值。可能是代码中的某个地方,
images
被覆盖了或其他什么。您可以设置断点并让一个监视程序监视该变量。PHP变量存在于服务器上,如何在Javascript代码中重新创建它?