Home > database >  Add variable inside foreach
Add variable inside foreach

Time:12-06

I intend to add the value of the valor variable. For that I'm using this code:

var data = [
   {Id: "552", valor:  "50.00", Descricao: "Fraldas", },
   {Id: "552", valor: "35.00", Descricao: "Creme", },
   {Id: "545", valor: "23.00", Descricao: "Caneta", },
   {Id: "545",  valor: "15.00", Descricao: "Caderno",  },
   {Id: "602", valor: "23.00", Descricao: "Caneta", },
   {Id: "602",  valor: "15.00", Descricao: "Caderno",  },
];

var results = data.reduce(function(results, org) {
  (results[org.Id] = results[org.Id] || []).push(org);
  return results;
}, {});

$(document).on('click', '.dad-pagamento', function() {

 var linha = ``;
  Object.keys(results).forEach(i => {
    linha  = `totalizando`;
    var ValorPacote = 0;
    Object.keys(results[i]).forEach(b => {
   
      valor = results[i][b].valor;
      
      ValorPacote = parseFloat(valor).toFixed(2)   parseFloat(valor).toFixed(2);
      linha  = `${ValorPacote}`;
    })
  })
  $('#minhaDiv1').show();
  $(".pagmfalta").html(linha);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button type="button" tabindex="0"  >Teste</button>

   <section id="s1">
      <div style="display:none" id="minhaDiv1">
        
         <button type="button" tabindex="0"  id="btnPrint" style="text-align:right;">Print</button>

         <div id="printThis">
            <div >
            
               
            </div>
         </div>

      </div>
   </section>

But the problem is that when I run the code it is not adding and without, repeating the value twice. Can anyone help?

CodePudding user response:

The problem is that toFixed() would return a string https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

You might want to first do the addition through (parseFloat(x) parseFloat(y)).toFixed(2) as follows

I hope this answers the question ( as I am not sure what was the initial question )

CodePudding user response:

If I understood correctly what you want to achieve then codes after this changes should be sufficient:

var data = [
   {Id: "552", valor:  "50.00", Descricao: "Fraldas", },
   {Id: "552", valor: "35.00", Descricao: "Creme", },
   {Id: "545", valor: "23.00", Descricao: "Caneta", },
   {Id: "545",  valor: "15.00", Descricao: "Caderno",  },
   {Id: "602", valor: "23.00", Descricao: "Caneta", },
   {Id: "602",  valor: "15.00", Descricao: "Caderno",  },
];

var results = data.reduce(function(results, org) {
  (results[org.Id] = results[org.Id] || []).push(org);
  return results;
}, {});

$(document).on('click', '.dad-pagamento', function() {

 var linha = ``;
  Object.keys(results).forEach(i => {
    linha  = `totalizando<br />`;
    var ValorPacote = 0;
    Object.keys(results[i]).forEach(b => {
      //console.warn(results[i], b, results[i][b].valor);
      valor = parseFloat(results[i][b].valor);
      ValorPacote  = valor;     
    })
    linha  = parseFloat(ValorPacote).toFixed(2)   '<br />';
  })
  $('#minhaDiv1').show();
  $(".pagmfalta").html(linha);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button type="button" tabindex="0"  >Teste</button>

   <section id="s1">
      <div style="display:none" id="minhaDiv1">
        
         <button type="button" tabindex="0"  id="btnPrint" style="text-align:right;">Print</button>

         <div id="printThis">
            <div >
            
               
            </div>
         </div>

      </div>
   </section>

What I fixed:

  1. valor is string so I ussed parseFloat to convert it to number
  2. I changed calulation of ValorPacote - now it is simple ValorPacore = valor
  3. I moved printing valorPacote to linha outside of loop

And basically it was that.

I think the most problem and confusion to you could be this string/number conversion.

  • Related