I have a small problem that I am having some difficulty in identifying the best method of transforming the array. I have an array of objects like so:
[{
genre: 'fiction',
books: ['book1', 'book2'],
},
{
genre: 'fiction',
books: ['book6', 'book51'],
},
{
genre: 'non-fiction',
books: ['book23', 'book34'],
},
{
genre: 'fantasy',
books: ['book241', 'book49'],
},
{
genre: 'thriller',
books: ['book67', 'book32'],
},
{
genre: 'fantasy',
books: ['book21', 'book99'],
}];
Using vanilla JS how best can I return a new array with items grouped by the "genre' property so I get the following result:
[{
genre: 'fiction',
books: ['book1', 'book2', 'book6', 'book51'],
},
{
genre: 'non-fiction',
books: ['book23', 'book34'],
},
{
genre: 'fantasy',
books: ['book241', 'book49', 'book21', 'book99'],
},
{
genre: 'thriller',
books: ['book67', 'book32'],
}];
I have been playing around with this in various for loops but I can't quite figure out a good solution.