简体   繁体   中英

Looping through array of object and add objects in JSON - Javascript

I have an api that runs a mysql query and get hourly results for the last 48 and creates an array of objects with those results in it.

api & query:

app.get('/api/countRealTimeServedObject48', function (req, res) {
    newConnection.query('SELECT count(*) AS countRealTimeServedNumber, date_format(created, \'%H:00 - %d/%m/%y\') AS countRealTimeServed48 \
    FROM mimesi_realtime.served_clips \
    WHERE created > NOW() - INTERVAL 48 HOUR \
    GROUP BY date_format(created, \'%H:00 - %d/%m/%y\') \
    ORDER BY created ASC', function (error, results, fields) {
        if (error) throw error;
        res.end(JSON.stringify(results));
    });
});

results:

[{"countRealTimeServedNumber":1,"countRealTimeServed48":"14:00 - 09/07/17"},
{"countRealTimeServedNumber":12,"countRealTimeServed48":"15:00 - 09/07/17"},
{"countRealTimeServedNumber":9,"countRealTimeServed48":"16:00 - 09/07/17"},
{"countRealTimeServedNumber":14,"countRealTimeServed48":"17:00 - 09/07/17"},
{"countRealTimeServedNumber":16,"countRealTimeServed48":"18:00 - 09/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"19:00 - 09/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"20:00 - 09/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"21:00 - 09/07/17"},
{"countRealTimeServedNumber":3,"countRealTimeServed48":"22:00 - 09/07/17"},
{"countRealTimeServedNumber":16,"countRealTimeServed48":"05:00 - 10/07/17"},
{"countRealTimeServedNumber":10,"countRealTimeServed48":"06:00 - 10/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"07:00 - 10/07/17"},
{"countRealTimeServedNumber":15,"countRealTimeServed48":"08:00 - 10/07/17"},
{"countRealTimeServedNumber":26,"countRealTimeServed48":"09:00 - 10/07/17"},
{"countRealTimeServedNumber":57,"countRealTimeServed48":"10:00 - 10/07/17"},
{"countRealTimeServedNumber":25,"countRealTimeServed48":"11:00 - 10/07/17"},
{"countRealTimeServedNumber":39,"countRealTimeServed48":"12:00 - 10/07/17"},
{"countRealTimeServedNumber":51,"countRealTimeServed48":"13:00 - 10/07/17"},
{"countRealTimeServedNumber":50,"countRealTimeServed48":"14:00 - 10/07/17"},
{"countRealTimeServedNumber":37,"countRealTimeServed48":"15:00 - 10/07/17"},
{"countRealTimeServedNumber":26,"countRealTimeServed48":"16:00 - 10/07/17"},
{"countRealTimeServedNumber":28,"countRealTimeServed48":"17:00 - 10/07/17"},
{"countRealTimeServedNumber":25,"countRealTimeServed48":"18:00 - 10/07/17"},
{"countRealTimeServedNumber":19,"countRealTimeServed48":"19:00 - 10/07/17"},
{"countRealTimeServedNumber":15,"countRealTimeServed48":"20:00 - 10/07/17"},
{"countRealTimeServedNumber":2,"countRealTimeServed48":"21:00 - 10/07/17"},
{"countRealTimeServedNumber":6,"countRealTimeServed48":"22:00 - 10/07/17"},
{"countRealTimeServedNumber":14,"countRealTimeServed48":"05:00 - 11/07/17"},
{"countRealTimeServedNumber":16,"countRealTimeServed48":"06:00 - 11/07/17"},
{"countRealTimeServedNumber":37,"countRealTimeServed48":"08:00 - 11/07/17"},
{"countRealTimeServedNumber":54,"countRealTimeServed48":"09:00 - 11/07/17"},
{"countRealTimeServedNumber":29,"countRealTimeServed48":"10:00 - 11/07/17"},
{"countRealTimeServedNumber":61,"countRealTimeServed48":"11:00 - 11/07/17"},
{"countRealTimeServedNumber":24,"countRealTimeServed48":"12:00 - 11/07/17"},
{"countRealTimeServedNumber":55,"countRealTimeServed48":"13:00 - 11/07/17"},
{"countRealTimeServedNumber":47,"countRealTimeServed48":"14:00 - 11/07/17"}]

What I need to do is to loop through the array and check where there are missing hours and, where there are missing hours, add the countRealTimeServed48 with "hour:00 - date/month/year" and the countRealTimeServedNumber = 0;

How could I do it with javascript after the JSON.stringify()?

EDIT part of the code I am using in my app:

app.get('/api/countRealTimeServedObject48', function (req, res) {
newConnection.query('SELECT count(*) AS countRealTimeServedNumber, date_format(created, \'%H:00 - %d/%m/%y\') AS countRealTimeServed48 \
FROM mimesi_realtime.served_clips \
WHERE created > NOW() - INTERVAL 48 HOUR \
GROUP BY date_format(created, \'%H:00 - %d/%m/%y\') \
ORDER BY created ASC', function (error, results, fields) {
    if (error) throw error;
    const moment = require('moment');
    const result = JSON.stringify(results); //I believe this might be the problem
    const DATE_FORMAT = 'HH:mm - DD/MM/YY';

    const startDate = moment();

    const dateForIndex = (date, index) =>
    date.clone().add(index, 'hour').format(DATE_FORMAT);

    const dates = Array(48)
    .fill()
    .map((val, index) => dateForIndex(startDate, index))
    .map(
        date =>
        result.find(el => el.countRealTimeServed48 === date) || {
            countRealTimeServedNumber: 0,
            countRealTimeServed48: date
        }
    );
    res.end(dates);
});

});

This should do the trick:

'use strict';

const moment = require('moment');

const result = [
  { countRealTimeServedNumber: 1, countRealTimeServed48: '14:00 - 09/07/17' },
  { countRealTimeServedNumber: 12, countRealTimeServed48: '15:00 - 09/07/17' },
  { countRealTimeServedNumber: 9, countRealTimeServed48: '16:00 - 09/07/17' },
  { countRealTimeServedNumber: 14, countRealTimeServed48: '17:00 - 09/07/17' },
  { countRealTimeServedNumber: 16, countRealTimeServed48: '18:00 - 09/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '19:00 - 09/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '20:00 - 09/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '21:00 - 09/07/17' },
  { countRealTimeServedNumber: 3, countRealTimeServed48: '22:00 - 09/07/17' },
  { countRealTimeServedNumber: 16, countRealTimeServed48: '05:00 - 10/07/17' },
  { countRealTimeServedNumber: 10, countRealTimeServed48: '06:00 - 10/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '07:00 - 10/07/17' },
  { countRealTimeServedNumber: 15, countRealTimeServed48: '08:00 - 10/07/17' },
  { countRealTimeServedNumber: 26, countRealTimeServed48: '09:00 - 10/07/17' },
  { countRealTimeServedNumber: 57, countRealTimeServed48: '10:00 - 10/07/17' },
  { countRealTimeServedNumber: 25, countRealTimeServed48: '11:00 - 10/07/17' },
  { countRealTimeServedNumber: 39, countRealTimeServed48: '12:00 - 10/07/17' },
  { countRealTimeServedNumber: 51, countRealTimeServed48: '13:00 - 10/07/17' },
  { countRealTimeServedNumber: 50, countRealTimeServed48: '14:00 - 10/07/17' },
  { countRealTimeServedNumber: 37, countRealTimeServed48: '15:00 - 10/07/17' },
  { countRealTimeServedNumber: 26, countRealTimeServed48: '16:00 - 10/07/17' },
  { countRealTimeServedNumber: 28, countRealTimeServed48: '17:00 - 10/07/17' },
  { countRealTimeServedNumber: 25, countRealTimeServed48: '18:00 - 10/07/17' },
  { countRealTimeServedNumber: 19, countRealTimeServed48: '19:00 - 10/07/17' },
  { countRealTimeServedNumber: 15, countRealTimeServed48: '20:00 - 10/07/17' },
  { countRealTimeServedNumber: 2, countRealTimeServed48: '21:00 - 10/07/17' },
  { countRealTimeServedNumber: 6, countRealTimeServed48: '22:00 - 10/07/17' },
  { countRealTimeServedNumber: 14, countRealTimeServed48: '05:00 - 11/07/17' },
  { countRealTimeServedNumber: 16, countRealTimeServed48: '06:00 - 11/07/17' },
  { countRealTimeServedNumber: 37, countRealTimeServed48: '08:00 - 11/07/17' },
  { countRealTimeServedNumber: 54, countRealTimeServed48: '09:00 - 11/07/17' },
  { countRealTimeServedNumber: 29, countRealTimeServed48: '10:00 - 11/07/17' },
  { countRealTimeServedNumber: 61, countRealTimeServed48: '11:00 - 11/07/17' },
  { countRealTimeServedNumber: 24, countRealTimeServed48: '12:00 - 11/07/17' },
  { countRealTimeServedNumber: 55, countRealTimeServed48: '13:00 - 11/07/17' },
  { countRealTimeServedNumber: 47, countRealTimeServed48: '14:00 - 11/07/17' }
];

const DATE_FORMAT = 'HH:mm - DD/MM/YY';

const startDate = moment('2017-07-09 14:00:00'); // To make it work with the provided sample data set. You'll probably want just moment() in your code.

const dateForIndex = (date, index) =>
  date.clone().add(index, 'hour').format(DATE_FORMAT);

const dates = Array(48)
  .fill()
  .map((val, index) => dateForIndex(startDate, index))
  .map(
    date =>
      result.find(el => el.countRealTimeServed48 === date) || {
        countRealTimeServedNumber: 0,
        countRealTimeServed48: date
      }
  );

console.log(dates);

I suggest replacing the database's NOW() function with the startDate calculated in your application to keep things in sync.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM