How to add a field of objects returned from firebase ( vuejs + firebase )

Asked

Viewed 49 times

0

Hello, I’m trying to develop an ERP platform with vuejs using firebase as a storage solution, I’m trying to add all the 'value' fields of the 'expense' collection documents that loads the 'Name' 'Value' 'Data' 'Description' fields to present a total this is my code to get the data from firebase:

ReceberDados:function(){
            fb.DesRef.get().then((querySnapshot)=> {
                querySnapshot.forEach((doc) => {
                   var result = doc.data();
                            console.log(result)
                   
                })
            })
        }

as a result of the console I have inserir a descrição da imagem aqui

How can I add up all the 'Value' fields? ( tips for project tbm are welcome )

1 answer

2

Using javascript you can make one map to return only the value and then make a reduce to add them Take this example:

let usuarios = [
    { nome: 'Diego', Valor: 23, empresa: 'Rocketseat' },
    { nome: 'Gabriel', Valor: 15, empresa: 'Rocketseat' },
    { nome: 'Lucas', Valor: 30, empresa: 'Facebook' },
];
const valorTotal = usuarios.map(u => u.Valor).reduce((total, valor) => total + valor);
     console.log(valorTotal);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.