0

I am trying to combine arrays of dates from firebase. I am retrieving the start date and end date on firebase and after that I have a method that gets all between the 2 dates.

firebase.database().ref("dummy_dates").once("value", snap => {
  if (snap.val()) {
    snap.forEach(snapShot => {
      console.log('start date: ' + snapShot.val().startTime + "\nend date: " + snapShot.val().endTime)
      this.enumerateDaysBetweenDates(snapShot.val().startTime, snapShot.val().endTime);
    });
  }
});

This method gets all the days between the 2 days I'm retrieving from firebase using the above firebase for loop:

enumerateDaysBetweenDates(startDate, endDate) {

    var dates = [];
    var finalDate = [];

    var currDate = moment(startDate).startOf('day');
    var lastDate = moment(endDate).startOf('day');

    while (currDate.add(1, 'days').diff(lastDate) < 0) {
      dates.push(currDate.clone().toDate());
    }
    console.log("All Dates: ", dates)

    return dates;
  }

This is how my console look like after running the code: enter image description here

This is my firebase structure: enter image description here

What I am trying to do is to merge/combine all the arrays of dates I am getting from my firebase list using the for loop to be one array with all dates

I have tried using the concat method but it did not work, instead it adds the same array on the same array times the number of index. I have also tried this, but it's the same method I'm familiar with(the concat method).

I need help in merging all my arrays from firebase to be in 1 array.

André Kool
  • 4,880
  • 12
  • 34
  • 44
Zack
  • 472
  • 7
  • 16
  • Where are you concating the output array inside the for loop? – Hassan Imam Nov 26 '18 at 11:45
  • I've removed that line, I will add it back just now but it's on the enumerateDaysBetweenDates() method outside the while loop. – Zack Nov 26 '18 at 11:47
  • Instead of using `array#forEach`, you can use `array#reduce` and concat your array. – Hassan Imam Nov 26 '18 at 11:50
  • Thanks, let me try that now. – Zack Nov 26 '18 at 11:53
  • Good luck, in case you face any issue revert here. – Hassan Imam Nov 26 '18 at 11:54
  • @HassanImam: the `snap` variable is a [Firebase `DataSnapshot`](https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot) and not an array. Unfortunately, while snapshot has a `forEach` method, it doesn't have the other list comprehension methods from an array (such a `reduce`, and `map`). – Frank van Puffelen Nov 26 '18 at 15:17
  • @FrankvanPuffelen, you're correct. I think i missed it yesterday and have been trying it for the whole night and couldn't find a way around the reduce method. I think there should be a way around the `forEach` method. – Zack Nov 27 '18 at 09:39
  • @HassanImam, I think Frank is correct in his remarks, a firebase snapshot does not have a reduce method. – Zack Nov 27 '18 at 09:40
  • @Zack I was not aware that Firebase doesn't contain reduce method, you can continue using froEach then. – Hassan Imam Nov 28 '18 at 14:06
  • Will do thanks @HassanImam – Zack Nov 28 '18 at 15:15

0 Answers0