循环遍历对象数组并在JSON中添加对象 - Javascript

时间:2017-07-11 13:17:28

标签: javascript mysql arrays json

我有一个运行mysql查询的api,并获取最后48个小时的结果,并创建一个包含这些结果的对象数组。

api&查询:

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));
    });
});

结果:

[{"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"}]

我需要做的是遍历数组并检查缺少小时的位置,并在缺少小时的地方,添加countRealTimeServed48 "hour:00 - date/month/year"countRealTimeServedNumber = 0;

如何在JSON.stringify()之后用javascript做到这一点?

修改 我在我的应用中使用的部分代码:

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);
});

});

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

'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);

我建议您使用应用中计算的NOW()替换数据库的startDate功能,以保持同步。