How to check if an object exists in a Javascript array?

I saw other questions and I tried the filter function, but I'm not sure if I use it correctly.

Essentially, I have an array of objects that looks like this:

[{"hour":"6 am", "date":"2012-12-01"},{"hour":"7 am", "date":"2012-12-01"}] 

I go through many days, and every day I go through the hours between 6 AM and 9 PM. If there is an hour in the above array (like an hour on that particular date). I want to mark it as available in a new object, which I then pass to a new array. Below is the code I'm currently using.

 for(var i = 0; i < dayCount; i++){//Loop through the days that exist in the schedule day = new Object(); day.date = Date.parse(startDate).add(i).days(); day.dayName = weekday[day.date.getDay()] day.hours = new Array(); for(var j = 6; j < 22; j++){ //Loop through hours of the day seeing if they're available/scheduled, etc. if(j<=12){ thisHour = j +' am'; } else{ thisHour = j-12 + ' pm'; } var thisIsAvailable = $(assignedHours).filter(function(){ return assignedHours.hour == thisHour && assignedHours.date == day.date.toString("yyyy-MM-dd"); }); var thisIsScheduled = 0; day.hours.push({hour: thisHour,available: thisIsAvailable, scheduled: thisIsScheduled}); } daysInSchedule.push(day); } 

A few notes. I use .toString () in the day.date property because it is formatted in the JS date format, and the value I compare with it is in the MYSQL date format. I warned (talking about old school debugging) thisIsAvailable.length, and I get 0 every time. Any ideas are welcome. Thanks!

EDIT: Just realized that I forgot to tell you everything very important. The array that I give you is contained in the variable Named assignHours. Sorry it didn't work out.

EDIT 2: To clarify, my question is a bit between two pieces of code. I am trying to see if one of the objects in this array corresponds to the day and hour in the loop, and the nested loop through which I execute. If so, I want to pass this to a new object, which I then insert into the array of hours of the day. If not, I pass the object anyway, but with a 0 value for its availability.

+4
source share
2 answers

I suspect your problem is how you use the jQuery filter method:

 var thisIsAvailable = $(assignedHours).filter(function(){ return assignedHours.hour == thisHour && assignedHours.date == day.date.toString("yyyy-MM-dd"); }); 

If I am not mistaken, assignedHours.hour will always be undefined, because it always refers to your assignedHours array (where you want it to refer to the current element that you are filtering, that is, the element inside assignedHours ).

You probably want to do something like this:

 var thisIsAvailable = $(assignedHours).filter(function(array_key){ return this.hour == thisHour && this.date == day.date.toString("yyyy-MM-dd"); }); 

Edit1: As @Kevin B pointed out, $.grep better for filtering arrays since it does not create a DOM wrapper around your array:

 $.grep(assignedHours, function(el) { return el.hour == thisHour && el.date == day.date.toString("yyyy-MM-dd"); }); 
+3
source

Advanced from the comment on the response to the OP request:

You should check this.hour and this.date in your filter function.

+2
source

Source: https://habr.com/ru/post/1443456/


All Articles