Receive records for a specific hour and on average relevant data

I call webservice and get the data. The data is in the format -

mail: " xyz@xyz.com " price: "9.5" name: "xyz" receiveddate: "1374484561920" 

I convert the date in milliseconds to the date and find the hour in which the price was something. Thus, each EnterDate has an hour ie 11, 12, 13, etc.

 for(var l=0; l<data.length; l++){ var dataDate = recs[l].receiveddate; dataDate = +dataDate; var eachEntryDate = new Date(+dataDate.toString()); eachEntryDate = eachEntryDate.toString(); eachEntryDate = parseInt(eachEntryDate.substr(16, 2)); hourlyRecs[l] = {hour:eachEntryDate, price:recs[l].price}; } 

Now I want to get the average price for each hour. that is, the average price, where the hour is 11.12, etc. The data is in random order. What is the best way to do this?

+4
source share
3 answers

You can do the following:

 hourlyRecs.sort(function(a,b) { return a.hour - b.hour; } ); var results = []; var value = {}; var sum = 0; var count = 0; var hour = hourlyRecs[0].hour; for(var i = 0; i < hourlyRecs.length; i++) { if (hourlyRecs[i].hour == hour) { sum += hourlyRecs[i].price; count++; } else { value.avg = sum / count; value.hour = hourlyRecs[i].hour; results.push(value); value = {}; sum = 0; count = 0; sum += hourlyRecs[i].price; count++; } } value.avg = sum / count; value.hour = hourlyRecs[i].hour; results.push(value); 

You will end up with an hour / avgPrice array.

Hope this helps

0
source

Good. Since everything needs to be done in JS, organize the data first.

Create 2 arrays with 24 elements - one for cost per hour and for counting . (or just one of two values)

When you scroll through the data, increase the corresponding value at each moment in time and increase the quantity.

Something like that:

 var counts = []; var costs = []; for(var i = 0; i < recs.length; i++) { // This is your current code var dataDate = recs[l].receiveddate; dataDate = +dataDate; var hour = new Date(+dataDate.toString()); hour = hour .toString(); hour = parseInt(hour .substr(16, 2)); // Extend it with this counts[hour]++; costs[hour] += recs[l].price; } 

And then just divide the costs into the account and you go. This way, you don't have to worry about sorting data or anything like that.

In addition, your clock selection function can be simplified if you simply divide the number of milliseconds by 3600000 - the number of mills in 1 hour and take an int from it.

 hour = parseInt(milliseconds / 3600000); 
0
source

Create an array that will contain all prices per hour .

Javascript

 /* Fill an array by hour, containing all the prices for that hour */ var average = []; for (var elem in hourlyRecs) { average[hourlyRecs[elem].hour].push(hourlyRecs[elem].price); } /* get the average per each hour*/ for (var hour in average) { var priceAverage = 0; var count = 0; for (var price in average[hour]) { priceAverage += average[hour][price]; count+=1; } average[hour]["priceAverage"] = parseFloat(priceAverage/count,10); } 

Average per hour # 11: average[11]["priceAverage"] .

0
source

Source: https://habr.com/ru/post/1494482/


All Articles