0

When i run this everything works correctly except for the array pushing. The console.log(notificationdata); shows that notification data get it's values updated correctly but then looking at console.log(notifications) I have 7 identical ones with values matching the last one from notificationdata. Somehow pushing to the array is not happening correctly and I seem unable to figure it out. Any ideas?

var notifications = [];
reminder.days.value = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
reminder.times = [00:00]

      var notificationdata = {
        title: "Nu är det dags att ta en dos",
        text: "Ta " + medication + " mot " + affliction + " nu.",
        smallIcon: "../images/DosAvi_badge.png",
        icon: "../images/DosAvi_icon.png",
        every: "week",
        foreground: true
      }
      notificationdata.id = reminder.id;
      for(const day of reminder.days.value){
        for(const time of reminder.times){
          notificationdata.firstAt = getNextDayOfTheWeek(day, new Date(`Mon Jan 01 2020 ${time}`));
          //notificationdata.firstAt = new Date(`Wen Feb 26 2020 21:55`);
          console.log(notificationdata);
          notifications.push(notificationdata);
        }
      }
      console.log(notifications)

      cordova.plugins.notification.local.schedule(notifications);
    }
1
  • You can try not using const in for of so the time and day might change. Change it to let or just var. Commented Feb 26, 2020 at 23:47

2 Answers 2

1

notificationdata is an object and inside your loop you're just changing a property of this object. The push to the array adds a reference of the object to the array. So you end up with an array of 7 references to the same object. To fix this you've to copy the object first:

      for(const day of reminder.days.value){
        for(const time of reminder.times){
          const copyNotificationdata = {
              ...notificationdata,
              firstAt: getNextDayOfTheWeek(day, new Date(`Mon Jan 01 2020 ${time}`))
          }
          notifications.push(copyNotificationdata);
        }
      }
Sign up to request clarification or add additional context in comments.

Comments

0

Because you don't create a new object and just reuse the same.

Javascript object variable only holds a reference to an object. It means that you always update the same data in the memory and your array holds 7 references to the same object.

You have to create a new object and insert it into your array:

for(const day of reminder.days.value){
    for(const time of reminder.times){
        const newNotificationdata = { ...notificationData };
        newNotificationdata.firstAt = getNextDayOfTheWeek(day, new Date(`Mon Jan 01 2020 ${time}`));
        notifications.push(newNotificationdata);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.