I am having a crazy hard time figuring this out, maybe because I almost never use the reduce method and I have been looking at this way too long but all I am trying to do here is group together an array of objects based on the product title.
const products = [
{
id: 1,
title: 't-shirt'
},
{
id: 2,
title: 't-shirt'
},
{
id: 3,
title: 'jeans'
}
]
const linked_products = products.reduce((prevProduct, product) => {
}, [])
products.forEach(product => {
// this is an array of objects
// it's an array of objects with the same title
product.linked_products = linked_products
})
Expected Output:
products = [
{
id: 1,
title: 't-shirt',
// here is the new property
linked_products: [
{ id: 2, title: 't-shirt'}
]
},
{
id: 2,
title: 't-shirt',
linked_products: [
{ id: 1, title: 't-shirt'}
]
},
{
id: 3,
title: 'jeans',
linked_products: []
}
]