我正在读一个cvs文件,一列包含unix时间戳。我想计算value1和value2,value 2和value3等之间的差异。我该怎么做?
答案 0 :(得分:0)
请参阅:Javascript code to parse CSV data
计算时间差只是在将数据导入数组后将其减去它们并使用parseInt将其解析为Number / long。
答案 1 :(得分:0)
阅读评论后编辑的代码段。似乎数据中的所有差异都是900毫秒,但这段代码应该适用于任何其他值。
需要注意的是,它假设“从高到低”排序,因为看似存在于数据中。
var csv = [
'1374225300,126.0,1662.0,379.0,337.0,1091.0,893.0',
'1374224400,84.0,1491.0,251.0,289.0,909.0,801.0',
'1374223500,72.0,1200.0,126.0,180.0,651.0,682.0 ',
'1374222600,84.0,1011.0,126.0,180.0,505.0,563.0 ',
'1374221700,72.0,718.0,84.0,109.0,295.0,441.0 '
//... etcetera
];
function getPairDifference( pair ) {
//"pair" is a zero-based integer.
// "0" will return a difference between csv rows "0" & "1"
// "1" will return a difference between csv rows "1" & "2"
// etcetera...
var firstVal = parseInt( csv[pair].split(",")[0] );
var secondVal = parseInt( csv[pair + 1].split(",")[0] );
return firstVal - secondVal;
}
for ( var i = 0; i < csv.length; i += 1) {
// Demo code to visualize numbers.
// Actual function call of interest is simply "getPairDifference( i )"
$( "<div></div>" ).text( "DIFF "+i+": " + getPairDifference( i ) ).appendTo( "body" );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>