how to sum values
- September 24, 2018
- 1 reply
- 4023 views
hi all,
I would like to read values from csv file, and count sum of all values in each column.
please look at code below and give me answer - why commented row is wrong??
(The goal is to create dynamic dataShape with dynamic names of its column.)
var fileRepository = Things["SystemRepository"];
var fileContent = fileRepository.LoadText({
path: "suma.csv" }
);
var rows = fileContent.split("\n");
var header = rows[0].split(",");
var columnsQty = header.length;
var myInfoTable = { dataShape: {fieldDefinitions : { } }, rows: [] };
var abc = 0;
var header_Sum;
for ( var i=0; i<2; i++) {
header_Sum = "Sum_" + i;
myInfoTable.dataShape.fieldDefinitions[header_Sum]={
name: header_Sum,
baseType: "NUMBER"
}
abc = abc + parseFloat(rows[i]);
};
//myInfoTable.rows.push({header_Sum:abc});
myInfoTable.rows.push({Sum_0:abc});
result = myInfoTable;
thx
gucio

