javascript - 如何提取保存在 for 中的值?...这是我的代码。但它不起作用
问题描述
var table = document.getElementById("table"),
sumVal = 0;
var aux = '';
function prueba() {
for (var i = 1; i < table.rows.length; i++) {
var sumVal = parseInt(table.rows[i].cells[1].innerHTML);
aux = sumVal;
console.log(sumVal);
}
}
解决方案
问题是循环的每次迭代,您都在覆盖aux
变量的值。您需要更改aux
为一个数组,以便您可以添加到它。这是一个
Javascript 数组教程,可能可以帮助您理解。
var aux = [];//the array
function prueba(){
for(var i = 1; i < table.rows.length; i++){
var sumVal = parseInt(table.rows[i].cells[1].innerHTML);
aux.push(sumVal);//add the value to the end of the array
}
}
prueba();
console.log(aux);//console.log the contents of the array
推荐阅读
- r - 如何在具有多个条件的 R 中应用 RLE() 函数?
- flutter - 在设备启动时运行后台服务
- c - 3 x 3 网格的阿贝尔沙堆的特征是什么?
- docker - docker:如何在 shell 入口点“sh -c”中使用 shell 变量?
- javascript - 将 props 传递给实现 react suspense 的 axios hook
- python - 队列空方法是否损坏?
- python - 使用 Tensorflow Keras 在 CPU 上并行训练 N 个网络
- python - 将 PYTHONPATH 添加到 .zprofile/.bash_profile
- python-3.x - 从单个列表生成多个列表
- flutter - 如何在 Visual Code 上定位 Flutter 和 Dart?