Skip to content
Advertisement

How to fill missing dates inside array of object using javascript?

I want to fill this object from today’s date to next 7 days. Here is my object

JavaScript

Expected output:

JavaScript

Here is the code to generate array of dates from today to next 7 days

JavaScript

How can I fill in the missing dates?

Advertisement

Answer

You can use this code to generate the dates array. You can pass the startDate and numberOfDays you need. In your case you can just dateRange(new Date(), 7)

JavaScript

You can use Array.prototype.map and return the check if date exists in obj.sessions using Array.prototype.find then return the object otherwise just return the same date. Array.prototype.find returns undefined if item doesn’t exist.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement