thranduil90
thranduil90

Reputation: 63

Javascript count daily elements from DB

I have a website that performs some queries to a database and then creates tables. Right now I need to build an array which contains, for every day, the number of occurrences (items). In order to do that I'm now trying to save the result of the query, a json, into a variable, and then parsing that variable, but that's not working. How can I do that? Here's my code:

var someVar = [];

connection.query("https://blahblabblah?param1=val&param2=val2", function(err, rows){
  if(err) {
    throw err;
  } else {
    setValue(rows);
  }
});

function setValue(value) {
  someVar = value;
  console.log(someVar);
}

dayCounts = someVar.reduce(function (result, order) {
    var day = moment(someVar.CreationDate).format("YYYY-MM-DD");
    if (!result[day]) {
        result[day] = 0;
    }
    result[day]++;
    return result;
}, {});

console.log(dayCounts);

The resulting dayCounts would be something similar to this:

{[2020-10-01, 160], //[day, sum of elements found in that day]
 [2020-10-02, 69],
[2020-10-03, 110],
[...]}

Right now I'm experiencing issues and I can't connect and perform that query. How can I achieve this result? Thank you

EDIT: my returning variable is this:

[{Id, Var1, Var2, CreationDate},
{1, 123, Var2, 2020-01-01},
{2, 1234, Var2, 2020-01-01},
{3, 12345, Var2, 2020-01-01},
{4, 1234, Var2, 2020-01-02},
{5, 321, Var2, 2020-01-02},
{6, 3214, Var2, 2020-01-02},
{7, 5432, Var2, 2020-01-03},
{8, 5431, Var2, 2020-01-03}]

Upvotes: 0

Views: 260

Answers (1)

Monsieur Merso
Monsieur Merso

Reputation: 2146

Here is the code that will count number of day occurences in 'someVar':

const someVar = [
{ Id: 1,  Var1: 123, Var2 : '', CreationDate: '2020-01-01'},
{ Id: 2,  Var1: 1234, Var2 : '', CreationDate: '2020-01-01'},
{ Id: 3,  Var1: 1235, Var2 : '', CreationDate: '2020-01-01'},
{ Id: 4,  Var1: 1236, Var2 : '', CreationDate: '2020-01-02'},
{ Id: 5,  Var1: 321, Var2 : '', CreationDate: '2020-01-02'},
{ Id: 6,  Var1: 13, Var2 : '', CreationDate: '2020-01-02'},
{ Id: 7,  Var1: 17, Var2 : '', CreationDate: '2020-01-03'},
{ Id: 8,  Var1: 138, Var2 : '', CreationDate: '2020-01-03'},
];


const result = someVar.reduce(function (accumulator, currentValue) {
    // first argument is accumualtor and second is element from someVar
    // we set initial state of accumulator (empty object) as 3rd argument to reduce method
    const day = moment(currentValue.CreationDate).format('YYYY-MM-DD');
    // check here for undefined, cause days with zero counts also will evaluate for false
    if (accumulator[day] === undefined) {
        accumulator[day] = 1;
    } else {
        accumulator[day]++;
    }
    return accumulator;
}, {} /* initial accumulator state */);

I think that problem was that you kept referencing 'someVar' in a reduce block and when you had to count days in if(!result[days]) was constantly 0 - first time you encounter a day it will initialize accumulator with 0 and next time when you have entry with same date it evaluates to false cause value associated with that day is 0.

To read more about reduce signature: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Upvotes: 1

Related Questions