Skip to content

Instantly share code, notes, and snippets.

@felipeLeao18
Last active August 30, 2022 21:19
Show Gist options
  • Save felipeLeao18/30d772cb6161d3a46f55af631601cd42 to your computer and use it in GitHub Desktop.
Save felipeLeao18/30d772cb6161d3a46f55af631601cd42 to your computer and use it in GitHub Desktop.
const products = {
compras: [
{
data: "2022-01-01",
produtos: [
{
cod: "a",
qtd: 2,
valor_unitario: 12.34
},
{
cod: "b",
qtd: 1,
valor_unitario: 3.99
},
{
cod: "c",
qtd: 3,
valor_unitario: 98.14
}
]
},
{
data: "2022-01-02",
produtos: [
{
cod: "a",
qtd: 6,
valor_unitario: 12.34
},
{
cod: "b",
qtd: 1,
valor_unitario: 3.99
},
{
cod: "c",
qtd: 1,
valor_unitario: 34.02
}
]
}
]
}
const sumProducts = () => {
const sum = products.compras.flatMap(({ produtos }) => produtos).reduce((acc, cur) => {
acc += cur.qtd * cur.valor_unitario
return acc
}, 0)
return sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment