Incorrect Printing of Values using Reduce

0

I'm having trouble implementing reduce in a calculation of an array. I'm following some examples of this link .

If I do exactly as it is in the documentation, using the static values of the array, it prints the value of the sum in my console . But when I pass my array of values to reduce , it returns the screen below, it seems to me that it is concatenating the value rather than performing the sum, does anyone know what might be wrong? Here are my functions to understand better:

//produtosMarcados = é um array
//valorProdutos = é um array

//Captura o produto selecionado, e captura o preço de cada item//
//Ambos formam duas matrizes, sendo uma matriz de nomes de produto
//e uma matriz de preço dos produtos//
if(produto.selecionado == true && produto.selecionado >= 1){
    this.produtosMarcados.push(produto);
    this.valorProdutos.push(produto.preco);
} 
this.venda.produtos = this.produtosMarcados;
//Imprimo os arrays no console e estão com os valores corretos
console.log(this.venda.produtos);
console.log(this.valorProdutos);
}


 //Variável total deve receber a minha matriz que é o valorProdutos
 //chamo o reduce e peço para retornar o valor de a + b
 let total = this.valorProdutos.reduce(function(a, b) {
    return a + b;
 });
 //imprimo no console e o resultado é 1020 ao invés de ser 30
 console.log(total);
    
asked by anonymous 17.09.2018 / 23:35

0 answers