-3

I have an array of date, how do I find the most common / second / third etc day of the week? The I have tried the following

moment(@date field).week() 

but how about an array of json dates?

2018-04-19
2018-04-19
2018-04-19
2018-04-20
2018-04-24
2018-05-02
2018-05-02
2018-05-02
Liam
  • 27,717
  • 28
  • 128
  • 190
David
  • 45
  • 3
  • 9
  • You can do this `const res = dates .map(v => v.split('-')) .reduce((acc, v) => { const idx = parseInt(v[1]); acc[idx] ? acc[idx] += 1 : acc[idx] = 1; return acc; }, []) .map((v, idx) => [idx, v]) .sort((a, b) => b[1] - a[1]); console.log(res);` – Matus Dubrava May 29 '18 at 09:55
  • 2
    Why this question was closed and down voted? I honestly don't understand, seems to me that also the question pointed is misleading, here the OP is using `moment` library so it doesn't need to get the day of the week, it want just to have the week's year for a set of days in an array, sorted by relevance basically. – ZER0 May 29 '18 at 09:57
  • I agree, the question was to sort days of week by frequency – Dominic May 29 '18 at 10:01
  • Isn't that for the OP to decide? He seemed happy with the duplicate until it was reopened? – Liam May 29 '18 at 10:17
  • @David are the answers what you were after, or were you looking to just get the day week as a string as the duplicate answer was about? – Dominic May 29 '18 at 10:36

2 Answers2

1

See Map, Date.prototype.getDay(), Spread Syntax, Array.prototype.sort(), Destructured Assignment, and Array.prototype.map() for more info.

// Input.
const dates = ['2018-04-19','2018-04-19','2018-04-19','2018-04-20','2018-04-24','2018-05-02','2018-05-02','2018-05-02']
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

// Occurences.
const occurrences = dates => [...dates.reduce((m, date) => {
  const day = days[new Date(date).getDay()]
  m.set(day, (m.get(day) || 0) + 1)
  return m
}, new Map)].sort((A, B) => B[1] - A[1])

// Output
const output = occurrences(dates)

// Proof.
console.log('With Counts', output)
console.log('Without Counts', output.map(([day]) => day))
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
0
  1. Get the days of the week
  2. Sort them by frequency (removing duplicates)
  3. Translate them to weekday string (you can also do this with moment or Intl.DateTimeFormat)

const dates = [
  '2018-04-19',
  '2018-04-19',
  '2018-04-19',
  '2018-04-20',
  '2018-04-24',
  '2018-05-02',
  '2018-05-02',
  '2018-05-02',
];

const daysOfWeek = dates.map(date => new Date(date).getDay());

// Now we have the days as numbers we can sort by frequency
// This function is from a google search: https://stackoverflow.com/a/3579651/414062
function sortByFrequency(array) {
  const frequency = {};

  array.forEach(value => frequency[value] = 0);
  const uniques = array.filter(value => ++frequency[value] == 1);
  return uniques.sort((a, b) => frequency[b] - frequency[a]);
}

const sortedDaysOfWeek = sortByFrequency(daysOfWeek);

// Now translate to weekday values
const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
const weekdaysByFrequency = sortedDaysOfWeek.map(day => weekdays[day - 1]);

console.log(weekdaysByFrequency);
Dominic
  • 62,658
  • 20
  • 139
  • 163
  • Ok fixed whoops – Dominic May 29 '18 at 10:14
  • why did you reopen this after the OP had marked it as a duplicate? – Liam May 29 '18 at 10:15
  • 1
    The duplicate was how to get the day of the week as a string, and this question is to sort days of week by most common occurrence. If the OP really did completely mistype the question then let me know and we can mark it as duplicate again @David – Dominic May 29 '18 at 10:23
  • But the OP marked it as a dupe and you over ruled them? That's my point. If they'd asked for it to be reopened that'd make sense, but they didn't? – Liam May 29 '18 at 10:24
  • 1
    Perhaps the OP was bullied into closing it from the downvotes? But the truth is I didn't see it was the OP who closed it, so I'm happy to close it again just waiting for his response – Dominic May 29 '18 at 10:26
  • 1
    I was under the impression that this was a duplicate question, I was wrong – David May 29 '18 at 11:35
  • 1
    Thank you guys for your guidance.. I got my issue resolved! – David May 29 '18 at 11:35