Created
August 30, 2022 19:01
-
-
Save vessaro/cee56249182da3cddee1f0713cc92c39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 compras = [ | |
{ | |
data: "2022-08-01", | |
produtos: [ | |
{ | |
cod: 1, | |
qtd: 10, | |
valor_unitario: 123, | |
}, | |
{ | |
cod: 2, | |
qtd: 20, | |
valor_unitario: 234, | |
}, | |
{ | |
cod: 3, | |
qtd: 30, | |
valor_unitario: 345, | |
}, | |
], | |
}, | |
{ | |
data: "2022-08-29", | |
produtos: [ | |
{ | |
cod: 4, | |
qtd: 4, | |
valor_unitario: 123.45, | |
}, | |
{ | |
cod: 5, | |
qtd: 5, | |
valor_unitario: 234.56, | |
}, | |
{ | |
cod: 6, | |
qtd: 6, | |
valor_unitario: 345.67, | |
}, | |
], | |
}, | |
]; | |
const somaDasCompras = compras | |
.flatMap((compra) => compra.produtos) | |
.map((produto) => produto.qtd * produto.valor_unitario) | |
.reduce((somaAnterior, somaAtual) => somaAnterior + somaAtual, 0); | |
console.log(somaDasCompras); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment