运行我的代码后
var fs = require('fs'),
readline = require('readline');
var rd = readline.createInterface({
input: fs.createReadStream('data.txt'),
output: process.stdout,
terminal: false
});
rd.on('line', function(line) {
var line;
var arr = line.split();
console.log(arr);
});
获得此回报:
[ 'n10,1366829460,1366831260,60|37.0,65.0,87.0,41.0,100.0,76.0,83.0,82.0,50.0,54.0,46.0,28.0,68.0,69.0,94.0,90.0,57.0,47.0,94.0,83.0,79.0,85.0,99.0,77.0,85.0,70.0,91.0,88.0,64.0,89.0' ]
[ 'n11,1366829460,1366831260,60|100.0,57.0,84.0,35.0,87.0,67.0,66.0,92.0,39.0,57.0,65.0,65.0,60.0,39.0,89.0,49.0,71.0,77.0,89.0,67.0,74.0,70.0,52.0,69.0,88.0,85.0,99.0,100.0,97.0,97.0' ]
[ 'n12,1366829460,1366831260,60|40.0,35.0,75.0,35.0,62.0,67.0,38.0,31.0,34.0,34.0,57.0,69.0,76.0,38.0,78.0,91.0,76.0,76.0,71.0,88.0,20.0,81.0,58.0,89.0,100.0,70.0,79.0,50.0,54.0,75.0' ]
[ 'n13,1366829460,1366831260,60|73.0,77.0,78.0,42.0,18.0,43.0,43.0,85.0,51.0,61.0,21.0,81.0,50.0,64.0,86.0,40.0,83.0,43.0,28.0,45.0,34.0,55.0,27.0,46.0,44.0,57.0,35.0,69.0,69.0,55.0' ]
我现在正在尝试打印出行名称(例如' n10'),然后找到每个数组中数字的平均值(例如' n10 :平均50.3')。有什么帮助吗?
答案 0 :(得分:1)
您必须遍历并解析数组。类似的东西:
var a = [[ 'n10,1366829460,1366831260,60|37.0,65.0,87.0,41.0,100.0,76.0,83.0,82.0,50.0,54.0,46.0,28.0,68.0,69.0,94.0,90.0,57.0,47.0,94.0,83.0,79.0,85.0,99.0,77.0,85.0,70.0,91.0,88.0,64.0,89.0' ]
[ 'n11,1366829460,1366831260,60|100.0,57.0,84.0,35.0,87.0,67.0,66.0,92.0,39.0,57.0,65.0,65.0,60.0,39.0,89.0,49.0,71.0,77.0,89.0,67.0,74.0,70.0,52.0,69.0,88.0,85.0,99.0,100.0,97.0,97.0' ]
[ 'n12,1366829460,1366831260,60|40.0,35.0,75.0,35.0,62.0,67.0,38.0,31.0,34.0,34.0,57.0,69.0,76.0,38.0,78.0,91.0,76.0,76.0,71.0,88.0,20.0,81.0,58.0,89.0,100.0,70.0,79.0,50.0,54.0,75.0' ]
[ 'n13,1366829460,1366831260,60|73.0,77.0,78.0,42.0,18.0,43.0,43.0,85.0,51.0,61.0,21.0,81.0,50.0,64.0,86.0,40.0,83.0,43.0,28.0,45.0,34.0,55.0,27.0,46.0,44.0,57.0,35.0,69.0,69.0,55.0' ]];
a = a.map(function(line){
return line.split(',');
});
然后,您可以在地图功能中对其进行任何数学运算。
答案 1 :(得分:0)
假设您的数据是“数组列表”,称为“myTable”
使用jQuery,您可以执行以下操作:
迭代您的数据:
var myTable = [ ['N10,1,2|1,2,3'], ['N11,1,2|5,6,7'] ];
$(myTable).each(function (iRow, row) {
var rowSplit = row[0].split('|');
var avgCount = 0;
var avgSum = 0;
$(rowSplit[1].split(',')).each(function (iVal, val) {
avgCount++;
avgSum += parseFloat(val);
});
var out = rowSplit[0].split(',')[0] + " avg " + (avgSum / avgCount);
document.body.innerHTML += out + "<br>";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
答案 2 :(得分:0)
我想我已经找到了数据模式......
假设:
var data = 'n10,1366829460,1366831260,60|37.0,65.0,87.0,41.0,100.0,76.0,83.0,82.0,50.0,54.0,46.0,28.0,68.0,69.0,94.0,90.0,57.0,47.0,94.0,83.0,79.0,85.0,99.0,77.0,85.0,70.0,91.0,88.0,64.0,89.0';
这是逗号分隔的值列表(CSV)。
已经指出,第一个值(例如&#39; n10&#39;)是每行数据的标签。
之后有两个大整数,它们似乎是日期时间,删除了最后三位数(毫秒)。当整数(后缀为3个零)转换为日期时,我发现日期之间的差异为一小时。
这可以解释下一个值60,就像两个时间戳之间的分钟数一样。
然而,这是CSV崩溃的地方。 60之后有一个竖线(|)字符,后面是逗号分隔的数字列表。我假设这些值是要平均的值。
假设所有这一切都是正确的,您可以轻松确定每行的后续行为。
首先,可以通过查找第一个逗号的索引并将所有角色提升到该点来提取标签:
var label = data.substr(0, data.indexOf(','));
接下来抓取管道后的所有文本,然后将字符串解析为数字:
var numberList = data.substr(data.indexOf('|')+1);
var numbers = JSON.parse('[' + numberList + ']');
var sum = numbers.reduce(function(runningSum, currNum) {
return runningSum + currNum;
}, 0);
var avg = sum / numbers.length;
console.log('row ' + label + ' has an average of ' + avg);
现在你所要做的就是在数据的每一行上循环这个逻辑
dataItems.forEach(function(data) {
...
});