in my javascript, i have an Array of Calendar objects.
Each Calendar objects has an array of CalendarEvent objects
Each CalendarEvent object has a property Date, Name.
i want to check, give a date, it its exists in a specific Calendar.
I wanted to see if i could use jquery.InArray() but it seems like i have to loop through every CalendarEvent object in the Calendar to find it.
is there any fast开发者_如何学Cer way to do this search ??
is there any faster way to do this search ??
You can build an index (e.g. a reverse lookup).
For example, your Calendar object could have an object of Date => CalendarEvent mappings. The key (the Date) must be serialized to a string (to be inserted into the object), so you may want to use the UNIX time as the key.
Given the structure you've outlined, the only way to detect whether a particular date exists is to loop through the array of Calender objects and then to subsequently loop through its events. As strager has mentioned, you could maintain an object mapping each date to a specific index or CalenderEvent, but this would presumably require modification of the Calender logic.
精彩评论