25 lines
643 B
TypeScript
25 lines
643 B
TypeScript
|
|
function splitByTitle(movies: Array<Movie>): { [s: string]: Movie[]; } {
|
|
const result = movies.reduce((r, a) => {
|
|
var letter = a.title[0].toUpperCase();
|
|
if (!isNaN(parseInt(letter))) letter = "#";
|
|
r[letter] = r[letter] || [];
|
|
r[letter].push(a);
|
|
return r;
|
|
}, Object.create(null));
|
|
|
|
return result;
|
|
}
|
|
|
|
function splitByYear(movies: Array<Movie>): { [s: string]: Movie[]; } {
|
|
const result = movies.reduce((r, a) => {
|
|
const year = new Date(a.released).getFullYear();
|
|
r[year] = r[year] || [];
|
|
r[year].push(a);
|
|
return r;
|
|
}, Object.create(null));
|
|
|
|
return result;
|
|
}
|
|
|
|
export { splitByTitle, splitByYear }; |