为什么getTotals()方法不起作用?

huangapple go评论54阅读模式
英文:

Why is the getTotals() method not working?

问题

I will only translate the code portion for you:

我正在创建一个财务应用程序每插入一项数据顶部的收入”,“支出余额表根据用户输入的值进行更新例如如果代码从用户那里接收到收入值那么ReceitasSaldo框必须更新并接收这些值目前代码无法正常工作我应该怎么做

normalizeNumbers(value) {
    const newValue = value.toLocaleString("pt-BR", {
        style: "currency",
        currency: "BRL"
    });
    return newValue;
}
getTotals() {
    const newReceitas = this.valores.filter(({ checked }) => {
        checked === '<i class="fa-sharp fa-solid fa-arrow-up arrowUp-icon"></i>';
    }).map(({ valor }) => +valor);
    console.log(newReceitas);
    const newDespesas = this.valores.filter(({ checked }) => {
        checked == '<i class="fa-sharp fa-solid fa-arrow-down arrowDown-icon"></i>';
    }).map(({ valor }) => +valor);
    console.log(newDespesas);
    const totalReceitas = newReceitas.reduce((acc, curr) => {
        return acc + curr;
    }, 0);
    const totalDespesas = newDespesas.reduce((acc, curr) => {
        return acc + curr;
    }, 0);
    const totalSaldo = +totalReceitas - +totalDespesas;
    this.receitas.innerText = this.normalizeNumbers(+totalReceitas);
    this.despesas.innerText = this.normalizeNumbers(+totalDespesas);
    this.saldo.innerText = this.normalizeNumbers(totalSaldo);
}

Please note that I've translated the code portion as requested, and I haven't included any additional information.

英文:

I'm creating a finance app that, with each item that is inserted, the "income", "expenses" and "balance" tables at the top are updated according to the values ​​entered by the user. For example, if the code receive a income value from the user, the "Receitas" and "Saldo" boxes have to be updated and receive these values. As it is, the code is not working. What should I do?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

normalizeNumbers(value) {
const newValue = value.toLocaleString(&quot;pt-BR&quot;, {
style: &quot;currency&quot;,
currency: &quot;BRL&quot;
});
return newValue;
}
getTotals() {
const newReceitas = this.valores.filter(({ checked }) =&gt; {
checked === &#39;&lt;i class=&quot;fa-sharp fa-solid fa-arrow-up arrowUp-icon&quot;&gt;&lt;/i&gt;&#39;;
}).map(({ valor }) =&gt; +valor);
console.log(newReceitas);
const newDespesas = this.valores.filter(({ checked }) =&gt; {
checked == &#39;&lt;i class=&quot;fa-sharp fa-solid fa-arrow-down arrowDown-icon&quot;&gt;&lt;/i&gt;&#39;;
}).map(({ valor }) =&gt; +valor);
console.log(newDespesas);
const totalReceitas = newReceitas.reduce((acc, curr) =&gt; {
return acc + curr;
}, 0);
const totalDespesas = newDespesas.reduce((acc, curr) =&gt; {
return acc + curr;
}, 0);
const totalSaldo = +totalReceitas - +totalDespesas;
this.receitas.innerText = this.normalizeNumbers(+totalReceitas);
this.despesas.innerText = this.normalizeNumbers(+totalDespesas);
this.saldo.innerText = this.normalizeNumbers(totalSaldo);
}
}

<!-- end snippet -->

enter image description here

答案1

得分: 2

Looking at the code above, since I don't know the values of the valores array, I presume it's because of the filter callbacks, which never return a value and return empty arrays.

Either add return:

({ checked }) => {
  return checked == '....'
}

or use an arrow function:

({ checked }) => checked == '....'
英文:

Looking at the code above, since I don't know the values of the valores array, I presume it's because of the filter callbacks, which never return a value and return empty arrays.

Either add return

({ checked }) =&gt; {
return checked == &#39;....&#39;
}

or use arrow function

({ checked }) =&gt; checked == &#39;....&#39;

TS Playground

huangapple
  • 本文由 发表于 2023年4月13日 21:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定