etAllCount listInformationnullthenresponse = thisinformationList = responserows; ;如何遍历这个informationList 集合相加其中的slQuantity值slQuantity为string类型
您可以使用Array.reduce()方法遍历informationList集合,并将其中的slQuantity值相加。由于slQuantity是字符串类型,我们需要在累加之前将其转换为数字类型。下面是示例代码:
let totalQuantity = this.informationList.reduce((acc, curr) => {
return acc + parseInt(curr.slQuantity);
}, 0);
console.log(totalQuantity); // 输出相加后的slQuantity总和
在上面的代码中,我们使用了reduce()方法对informationList进行遍历。reduce()方法接受两个参数:一个回调函数和一个初始值。回调函数接受两个参数:累加器(acc)和当前值(curr)。我们在回调函数中将累加器和当前值的slQuantity相加,并返回新的累加器值。初始值为0,表示累加器的初始值为0。最后,我们将累加器的值打印出来,即为slQuantity的总和。
原文地址: https://www.cveoy.top/t/topic/7bc 著作权归作者所有。请勿转载和采集!