问题描述:

I am trying to do a calculation on the values array from the map function but when i perform calculations on the elements of values they are wrong yet when i interrogate the array itself the values are correct. Here is my map function :

function Map() {

var tempDate = new Date(Date.parse(this.date));

var day = Date.UTC(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate());

var sensorTime = Date.UTC(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate(),tempDate.getHours(), tempDate.getMinutes(), tempDate.getSeconds(), tempDate.getMilliseconds());

sensorTime = sensorTime - day;

emit(

day,

{time : sensorTime,

seq : parseInt(this.seq_no) }

);

}

in the reduce function I sort the values array by sensorTime and then try to subtract the first element.seq from the last element.seq but I always get an incorrect answer. :-

function Reduce(key, values) {

var len = values.length;

var a = 0;

var b = len -1;

// ascending order

function SortByTime(x,y) {

return parseInt(x.time) - parseInt( y.time);

}

values.sort(SortByTime);

return {time : 0, seq : (values[b].seq - values[0].seq)};

}

if I just return one of the values without the calculation then it is correct. I have tried forcing type to number but didn't make any difference. I am sure I am missing something obvious.

thanks

相关阅读:
Top