jQuery $ .inArray()无法与jQuery makeArray()制成的数组一起正常使用 [英] jQuery $.inArray() not working properly with array made with jQuery makeArray()

查看:148
本文介绍了jQuery $ .inArray()无法与jQuery makeArray()制成的数组一起正常使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法创建了一个日期数组:

I created an date array using this:

 var holidays = ["7/24/2010","7/25/2010"];
 var holidaysArray = jQuery.makeArray(holidays);

,然后进行测试以查看数组中是否存在myDate(日期对象):

and then testing to see if myDate (a date object) exists in the array:

if ($.inArray(myDate, holidaysArray ) == -1) {....}

尽管myDate是两天之一,但是测试始终返回-1.我试图避免使用日期字符串进行测试.

However the test always return -1 even though myDate is one of the two days. I was trying to avoid using date strings to do the test.

如何将inArray函数与日期对象和日期对象数组一起使用? (我不确定holidayArray是否实际上是日期对象的数组,也许这就是测试失败的原因.)

How can I use inArray function with date objects and array of date objects? (I am not sure if holidaysArray is actually an array of date objects and maybe that's why the test is failing.)

推荐答案

我认为您正在比较DateString对象,这就是为什么您总是出错的原因.

I think you are comparing Date and String objects, that's why you'll get always false.

请参阅:

new Date("12/12/2000") == "12/12/2000" // this is false

还!请注意:

new Date("12/12/2000") == new Date("12/12/2000") // this is false too!

您应该使用日期时间值来比较日期

You should compare dates using their epoch time value like this

new Date("12/12/2000").valueOf() == new Date("12/12/2000").valueOf() // this is TRUE

这篇关于jQuery $ .inArray()无法与jQuery makeArray()制成的数组一起正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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