Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Loop through object of dates and retrieve all the days depending on the parameter

$
0
0

I'm trying to display some days that are not in the object below along with it. I have a response of array of objects

[
   {date: "2019-11-7"}, 
   {date: "2019-11-7"},
   {date: "2019-11-13"},
   {date: "2019-11-15"},
]

I want to display blank days that doesnt have the date in the object above so it will look like:

1 - empty
2 - empty
3 - empty
4 - empty
5 - empty
6 - empty
7 - Not
7 - Not
8 - empty
9 - empty
...etc
13 - Not
14 - empty
15 - Not
...etc

I have tried the loop 20 days as parameter below but it gives me wrong output like:

for (var x=1; x<=20; x++) {
    _.forEach(response, function(r, i){
        var check = moment(r.start, 'YYYY/MM/DD');
        var day   = check.format('D');
        var month   = check.format('M');

        // I just sample november which is 11
        if (month == 11) {
            if (x != day) {
                console.log(x, "empty")
            } else {
                console.log(x, "not")
            } 
        }


    })
}

The output is repeated 4 times as I have as I have 4 objects, but I wan't to only repeat once if not found. Also the dates that are found in the object are combined with empty and not. As I have understood that it loops through the entire response.

6 - empty
7 - Not
7 - Not
7 - Empty
8 - empty
9 - empty
...etc
13 - Empty
13 - Empty
13 - Not
14 - empty
15 - Not
 ...etc

Any lodash or javascript solution will be a help. I don't know how to accomplish the right way using it.


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>