Reputation: 4338
I have a list of objects, in each object I have a date and time.
onClick
of a td
element (its a calendar) I check the time on the calendar, then check if it matches any times in my list of objects.
onSelect: function(date) {
for (var i = 0; i < dateArray.length; i++) {
for (var prop in dateArray) {
if (date === dateArray[prop].date) {
console.log(dateArray[prop].time);
}
}
}
}
So I loop through my array, which is an array of objects. To be able to get anything out I need to do a for in loop, and in there I do a condional statement to say if the dates match. The console.log out the corect time.
However, the time gets console logged out 10 times since it's in a loop. But the only way I could get inside my object array was to loop through them.
How should I actually be doing this.
EDIT
dateArray structure: list of objects like [ object, object, object, object ]
and once I loop through them, within each object it looks like :
Object {title: "The Title", date: "01/01/2017", time: "07:30pm", available: true,}
Upvotes: 0
Views: 326
Reputation: 2022
I think you are simply looping twice.
Given it is an array, you should loop it using for, like this
onSelect: function(date) {
for (var i = 0; i < dateArray.length; i++) {
if (date === dateArray[i].date) {
console.log(dateArray[i].time);
}
}
}
Upvotes: 2