ionic2 angular2总和列中的值

时间:2017-11-06 21:44:42

标签: angular ionic2

我有一个向用户提供发票的应用程序,除了发票中列出的项目的总价值之外,我已经设置了所有内容,我使用以下代码对值进行求和,但不是得到总和,而是得到了连接值

JObject

显示值为0100035004000(假设为1000 + 3500 + 4000)

2 个答案:

答案 0 :(得分:0)

通过添加+:

将items.amount解析为数字
getTotal() {
let total = 0;
for (var i = 0; i < this.items.length; i++) {
    if (this.items[i].amount) {
        total += +this.items[i].amount;
        this.totalAmount = total;
    }
}
return total;
}

答案 1 :(得分:0)

而不是total += this.items[i].amount;似乎将数字连接成字符串,而是将其更改为total += Number(this.items[i].amount);,以便在typescript中将字符串转换为数字。