views:

39

answers:

2

how to insert values in a html table, an array multidimensional in javascript.

Additionally, such as performing calculations between the columns of the array and insert the results into a column of the same array.

thanks for your answers.

+1  A: 

Javascript code:

var multiArray;
window.onload = function() {
    // load the table into the multidimensional array.
    multiArray = [];
    var trs = document.getElementsByTagName('TR');
    for(var i = 0; i < trs.length; i++) {
        var arr = [];
        var tds = trs[i].childNodes;
        for(var j = 0; j < tds.length; j++) {
            var td = tds[j];
            if (td.tagName === 'TD') {
                arr.push(td.innerHTML);
            }
        }
        multiArray.push(arr);
    }
    // perform some calculations between the columns of the array
    var resCalc = [];
    for(i = 0; i < multiArray.length; i++) {
        resCalc[i] = 0;
        for(j = 0; j < multiArray[i].length; j++) {
            resCalc[i] += multiArray[i][j];
        }
    }
    // insert the results into a column of the same array
    var columnToModify = 0; // the index of the column you want to change
    for(i = 0; i < multiArray.length; i++) {
        multiArray[i][columnToModify] = resCalc[i];
    }
};
Protron