Last active
August 30, 2022 21:19
-
-
Save felipeLeao18/30d772cb6161d3a46f55af631601cd42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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