Skip to content

Instantly share code, notes, and snippets.

@jasp402
Created July 2, 2021 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasp402/bfa1c410885a742d860ab0ba081e2d40 to your computer and use it in GitHub Desktop.
Save jasp402/bfa1c410885a742d860ab0ba081e2d40 to your computer and use it in GitHub Desktop.
reordenamiento de un array segun los dias de la semana y las horas dada.
const days = ["Lun","Mar","Mie","Juv","Vie","Sab","Dom"]
const userSchedule = [
[],
['17:00', '18:00', '19:00'],
['17:00', '18:00', '19:00'],
['17:00'],
['17:00', '18:00', '19:00'],
['17:00', '18:00', '19:00'],
[]
];
const result = userSchedule.reduce((acc={}, item, i) => {
item.forEach(x =>acc[x] = acc[x] ? acc[x].concat(days[i-1]) : [days[0]]);
return acc
},{});
console.log(result);
/*
{
'17:00': [ 'Lun', 'Mar', 'Mie', 'Juv', 'Vie' ],
'18:00': [ 'Lun', 'Mar', 'Juv', 'Vie' ],
'19:00': [ 'Lun', 'Mar', 'Juv', 'Vie' ]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment