节点JS-将数据库中的行格式化为JSON

时间:2018-11-19 17:16:38

标签: mysql arrays node.js json sqlite

company有50列。前5列为time, company_type, company_id, status, established_date。接下来的45列是:employee0, employee1, employee2, ..., employee44。这45列的值为integer类型。我需要将这45列的格式设置为JSON,如下所示:

'{"employee0":"2", "employee1":"4", "employee2":"6", ... , "employee44":"101"}'

在下面的脚本中,我创建了一个包含雇员的变量chRowchRowi串联,每次运行时递增。但是,row.temp(实际上应该是row.employee<i>)给出了unspecified,而chRow则显示为chRow,而不是每次迭代都分配给它的值。非常感谢我如何创建JSON数组的任何帮助。

    db.each(`SELECT * FROM company`, (err, row) => {
                    if (err) { throw err; }

                    var i;
                    var chRow = "employee";
                    var jsonArr = [];
                    for (var i = 0; i < (row.length-5); i++) {
                        temp = chRow+i
                        jsonArr.push({
                            chRow : row.temp //I would like this to be as follows - channel0: 2 or channel1: 4 or so on depending on i value
                        });
                    }
                    JSON.parse(jsonArr);
    });

0 个答案:

没有答案