Skip to content

Instantly share code, notes, and snippets.

@humansonofhuman
Created September 1, 2020 18:49
Show Gist options
  • Save humansonofhuman/c26cff05c929ae1b88403c7ec11def5b to your computer and use it in GitHub Desktop.
Save humansonofhuman/c26cff05c929ae1b88403c7ec11def5b to your computer and use it in GitHub Desktop.
const data = [
{
idArtisan: 1,
idProduct: 3,
name: 'Aguila de madera',
price: 800.00,
quantity: 3,
weight: 0.25
},
{
idArtisan: 3,
idProduct: 16,
name: 'Portavasos - Cup holders',
price: 199.00,
quantity: 3,
weight: 0.50
},
{
idArtisan: 1,
idProduct: 15,
name: 'Corona de Barro - Clay Crown',
price: 699.00,
quantity: 1,
weight:0.15
},
{
idArtisan: 3,
idProduct: 20,
name: 'Craneo de Barro',
price: 1250.00,
quantity: 1,
weight: 1.00
}
]
data.map(x => ({
idArtisan: x.idArtisan,
price: x.price * x.quantity,
weight: x.weight * x.quantity,
})).reduce((acum, curr, i) => {
var iia = acum.findIndex(x => x.idArtisan === curr.idArtisan);
if (iia > -1) {
acum[iia].price += curr.price;
acum[iia].weight += curr.weight;
} else {
acum.push(curr)
}
return acum;
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment