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

循环遍历对象数组并在JSON-Javascript中添加对象,javascript,mysql,arrays,json,Javascript,Mysql,Arrays,Json,我有一个api,它运行一个mysql查询,获得过去48个小时的每小时结果,并创建一个包含这些结果的对象数组 api和查询: app.get('/api/countRealTimeServedObject48', function (req, res) { newConnection.query('SELECT count(*) AS countRealTimeServedNumber, date_format(created, \'%H:00 - %d/%m/%y\') AS count

我有一个api,它运行一个mysql查询,获得过去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);
});

}))

这应该可以做到:

“严格使用”;
恒力矩=要求的(‘力矩’);
常数结果=[
{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'}
];
施工日期格式='HH:mm-DD/mm/YY';
常数起始日期=时刻('2017-07-09 14:00:00');//使其与提供的样本数据集一起工作。您可能只需要在代码中使用moment()。
const dateForIndex=(日期,索引)=>
date.clone().add(索引,'hour').format(日期\格式);
常量日期=数组(48)
.fill()
.map((val,index)=>dateForIndex(startDate,index))
.地图(
日期=>
result.find(el=>el.countRealTimeServed48==date)|{
countRealTimeServedNumber:0,
countRealTimeServed48:日期
}
);
控制台日志(日期);

我建议将数据库的
NOW()
函数替换为应用程序中计算的
startDate
,以保持同步。

需要在字符串化或在浏览器中执行之前执行此操作。请提供一份报告。这个查询实际上是毫无意义的,它是我们看不到的结果数据。很抱歉,我之前没有正确链接结果图片,请勿以图片形式发布…没有人可以从图片中复制该图片,而且它更易于阅读格式化文本我将其添加为代码,可以吗?OP似乎没有使用矩库。这一点我们还不知道!;-)但我认为进行某种日期计算是不可避免的,我强烈建议不要重新进行这些计算……如果我将其插入我的应用程序,它会显示“result.find不是函数”。顺便说一句,很好,我使用的是moment.js。如果你看一下编辑,你会看到我在外观上想要做什么
stringify()
数组
转换为
字符串
,该字符串没有
find()
方法。如果您使用的是Express,则根本不需要将结果字符串化。只需使用将其作为JSON发送到浏览器。我想这就是你想做的,不是吗?请参见此示例(未测试):