JSFiddle - React, Tailwind, and code Playground
JavaScript
const products = [
{ title: 'test product', storefront: {id: 0, title: 'test storefront 1'} },
{ title: 'test product 2', storefront: {id: 0, title: 'test storefront 1'} },
{ title: 'test product 3', storefront: {id: 1, title: 'test storefront 2'} }
]
const rtn = products.reduce((p,c,i,a) => {
let current = p.find(store => store.storefront.id == c.storefront.id);
if (!current) {
current = { storefront: {id: c.storefront.id, title: c.storefront.title, products: []} }
p.push(current);
}
current.storefront.products.push(c)
return p;
}, [])