Skip to content

Instantly share code, notes, and snippets.

@santospatrick
Last active September 26, 2018 11:35
Show Gist options
  • Save santospatrick/162e43f6c25f235020e248826fc32ddf to your computer and use it in GitHub Desktop.
Save santospatrick/162e43f6c25f235020e248826fc32ddf to your computer and use it in GitHub Desktop.
const names = cultures.map(item => item.cultureName)
// ['Soybean', 'Maize', 'Wheat']
const energies = cultures
.reduce((acc, nextObject) => {
const nextValue = nextObject.energy.quantity;
const currentValue = acc[nextObject.energy.unit]
? acc[nextObject.energy.unit].quantity
: 0;
acc[nextObject.energy.unit] = {
quantity: currentValue + nextValue,
};
return acc;
}, {})
// { kJ: { quantity: 1728 }, g: { quantity: 0 } }
const totalQuantity = cultures
.filter(item => item.quantity)
.reduce((acc, nextObject) => acc + nextObject['quantity'], 0);
// 2700.80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment