[Solved] How do I get the total count of reminders mode by phone using javascript? [duplicate]


This could be achieved like so:

const data = [{ "coachName": "Angela", "coachId": 94253, "reminders": [{ "day": "Tuesday", "mode": "Text" }, { "day": "Tuesday", "mode": "Phone" }, { "day": "Tuesday", "mode": "Text" }, { "day": "Tuesday", "mode": "Text" }, { "day": "Tuesday", "mode": "Text" }, { "day": "Tuesday", "mode": "Phone" }, { "day": "Wednesday", "mode": "Text" }, { "day": "Wednesday", "mode": "Text" }, { "day": "Wednesday", "mode": "Text" }, { "day": "Wednesday", "mode": "Text" }, { "day": "Wednesday", "mode": "Text" }] }, { "coachName": "Melanie", "coachId": 94254, "reminders": [{ "day": "Wednesday", "mode": "Phone" }, { "day": "Wednesday", "mode": "Text" }, { "day": "Wednesday", "mode": "Phone" }, { "day": "Monday", "mode": "Text" }, { "day": "Monday", "mode": "Phone" }, { "day": "Monday", "mode": "Text" }, { "day": "Monday", "mode": "Text" }, { "day": "Monday", "mode": "Text" }, { "day": "Monday", "mode": "Phone" }, { "day": "Monday", "mode": "Text" }] }];

const countRemindersByMode = (reminders, mode, day) => {
  return reminders
    .filter((reminder) => reminder.mode === mode
      && (!day || reminder.day === day))
    .length;
};

const printReminders = (mode, day) => {
  let totalPhoneRemindersCount = 0;
  data.forEach((coach) => {
    const count = countRemindersByMode(
      coach.reminders,
      mode,
      day);
    totalPhoneRemindersCount += count;
    console.log(`${coach.coachName}: ${count}`);
  });
  console.log(`Total phone reminders: ${totalPhoneRemindersCount}`);
};

console.log('All reminders with mode Phone');
printReminders('Phone');

console.log('All reminders for Wednesday with mode Phone');
printReminders('Phone', 'Wednesday');

Please let me know if this helps.

2

solved How do I get the total count of reminders mode by phone using javascript? [duplicate]