Localstorage, Vuejs. How to recover and add up the value of a Jason/localstorage column

Asked

Viewed 30 times

0

I want to locally persist all sums(val*times) and sum all "sum" results using vuejs.

Data to be persisted:

data: {
      nameApp: 'App',
      somaValor: {
        id: '',
        val: '',
        vezes: '',
        soma: ''
      },     
    },

Data from the localstorage

localstorage já persistido

My code

somarTotal(){
    let somaValores = localStorage.getItem('contactsApp');
    somaValores = JSON.parse(somaValores);   
    var t = 0; 
    var total = 0;

//Aqui quero o loop para pegar os valores da coluna soma e trazer o total;

for (var i=0; i<5; i++) {
        t = somaValores.soma;
                tarray[i] = t;
                console.log(tarray);
          
       return somaValores.push(soma);
}

for (var i=0; i<5; i++) { 
  total += tarray[i];               
                          
      }


  console.log(total);
    }

Remark: I tried the reduce but it didn’t work, also error length..

Good people, I solved so:

computed: { 
total: function(){ 
var br = JSON.parse(numbers); \\ aqui peguei o array // aqui usei o metodo reduce 
var g = br.reduce(function(previous, current) { return previous + current.soma; }, 0); } 

Dai sums the values of my Array and in my tamplate I add it so {{total}}

  • Your question is unclear. Why are you doing direct attribution tarray[i] = t instead of using tarray.push(t)? Why are you returning (return) in the first iteration of for?

  • Hi, thanks for the tip. I got it in Vuejs as follows. computed: { total: Function(){ var br = JSON.parse(Numbers); here I got the array // here I used the reduce var g = br.reduce(Function(Previous, Current) { Return Previous + Current.soma; }, 0); Dai sums the values of my Array and in my tamplate I add the sum thus {total}}

  • Gesser, do not put your solution in the question, put it as answer (below the question field, at the bottom of the page, you will have a button "Answer your question").

No answers

Browser other questions tagged

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