简体   繁体   English

在 foreach 中添加变量

[英]Add variable inside foreach

I intend to add the value of the valor variable.我打算添加valor变量的值。 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: "C.neta", }, {Id: "545", valor: "15.00", Descricao: "Caderno", }, {Id: "602", valor: "23.00", Descricao: "C.neta", }, {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" class="dropdown-item btn-show dad-pagamento" >Teste</button> <section id="s1"> <div style="display:none" id="minhaDiv1"> <button type="button" tabindex="0" class="dropdown-item" id="btnPrint" style="text-align:right;">Print</button> <div id="printThis"> <div class="row pagmfalta"> </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?谁能帮忙?

The problem is that toFixed() would return a string https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed问题是 toFixed() 会返回一个字符串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您可能希望首先通过(parseFloat(x) + parseFloat(y)).toFixed(2)进行加法,如下所示

I hope this answers the question ( as I am not sure what was the initial question )我希望这能回答问题(因为我不确定最初的问题是什么)

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: "C.neta", }, {Id: "545", valor: "15.00", Descricao: "Caderno", }, {Id: "602", valor: "23.00", Descricao: "C.neta", }, {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" class="dropdown-item btn-show dad-pagamento" >Teste</button> <section id="s1"> <div style="display:none" id="minhaDiv1"> <button type="button" tabindex="0" class="dropdown-item" id="btnPrint" style="text-align:right;">Print</button> <div id="printThis"> <div class="row pagmfalta"> </div> </div> </div> </section>

What I fixed:我修复了什么:

  1. valor is string so I ussed parseFloat to convert it to number valor是字符串,所以我使用parseFloat将其转换为数字
  2. I changed calulation of ValorPacote - now it is simple ValorPacore += valor我更改了 ValorPacote 的计算 - 现在很简单ValorPacore += valor
  3. I moved printing valorPacote to linha outside of loop我将打印valorPacote移到循环外的linha

And basically it was that.基本上就是这样。

I think the most problem and confusion to you could be this string/number conversion.我认为对您来说最大的问题和困惑可能是这种字符串/数字转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM