Skip to content

Instantly share code, notes, and snippets.

@jasonleehodges
Last active September 14, 2020 17:23
Show Gist options
  • Save jasonleehodges/bc7926c20a55ee745cb51f25b60635e7 to your computer and use it in GitHub Desktop.
Save jasonleehodges/bc7926c20a55ee745cb51f25b60635e7 to your computer and use it in GitHub Desktop.
const numbers: number[] = [3, 5, 7, 9, 12];
const accumulated = numbers.reduce(
(accumulator: number, number: number) => accumulator + number,
0);
// 36
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wendesday",
"Thursday",
"Friday",
"Saturday"
];
const week = days.reduce(
(accumulator: { [dayOfWeek: string]: number }, day: string, index: number) => {
return {
...accumulator,
[day]: index
}
},
{});
// {
// Sunday: 0,
// Monday: 1,
// Tuesday: 2,
// Wendesday: 3,
// Thursday: 4,
// Friday: 5,
// Saturday: 6
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment