Skip to content Skip to sidebar Skip to footer

Do The Same Than This Sql Query But In Javascript

I have a sql query that take the average in seconds between each datetime and group it by interval/10 (to have less precision for groupping I guess) and return the interval with th

Solution 1:

As you can see in the subject :

    // WIP
var helper = {};

// Count each time an interval occur in values
for (var i = 1; i < p_table.length; i++) {
  var d1 = new Date(p_table[i].datetime);
  var d2 = new Date(p_table[i - 1].datetime)
  var curr_interval_millis = d1 - d2;
  var curr_interval_seconds = curr_interval_millis / 1000;
  var key = Math.floor(curr_interval_seconds / 10) * 10; // We group by dozen of seconds and then put it back
  if (!helper[key]) helper[key] = 1;
  else helper[key] += 1;
}

console.log("Count each time an interval occur in values")
console.log(helper);

// Get the interval that occur the most often
var maxKey = -1;
var maxCount = 0;
Object.keys(helper).forEach(key => {
  var currMaxCount = helper[key];
  if (currMaxCount > maxCount) {
    maxKey = key;
    maxCount = currMaxCount;
  }
});

console.log("max", maxKey, helper[maxKey])

Post a Comment for "Do The Same Than This Sql Query But In Javascript"