When building objects using reduce, I often have crappy code like this:
function mapArticlesByTagAndId(articles) {
return articles.reduce((collector = {}, article) => {
const { tag, id } = article;
if(!collector[tag]) collector[tag] = {};
collector[tag][id] = article;
return collector;
})
}
I would love to one line this, but I can't figure out a way to do it. If only assigning a property to undefined created an object =P [Obviously this would cause different problems haha]
reduce()
. \$\endgroup\$