【问题标题】:Print only JSON Values returned from MySql query in nodejs仅打印从 nodejs 中的 MySql 查询返回的 JSON 值
【发布时间】:2018-10-01 00:02:25
【问题描述】:

我想从 mysql 中检索记录并在我的页面上显示以 nodejs 格式编码的数据

 [
    ["1262889000000",788,803,783.3,795.65],
    ["1262889000000",788,813,783.3,795.65],
    ["1451500200000",2.85,13,1.05,1.1],
    ........
 ]

我的表架构

Column1           |   col2   |   col3   |    col4     |   col5

1262889000000          788       783.3        794          65  
1262889000000          788       813.7        795          65 
...........
........... 

这是我的 nodejs 代码

app.get('/charts/sample1', function(req, res) {

    con.query("select col1,col2,col3,col4,col5 from table1", function(err, data) {
    res.send(data);
    }); 

 });

然而,目前当我运行我的代码时,我会在 json 中使用 {key1:value,key2:value,...},

【问题讨论】:

    标签: mysql arrays json node.js


    【解决方案1】:

    所以你想将每一行中的对象值映射到一个数组?

    这样的? :

    var rows = [{
        Column1: 1262889000000,
        col2: 788,
        col3: 783.3,  
        col4: 794,
        col5: 65
    }];
    
    var result = rows.map((row) => {
        return Object.values(row);
    });
    
    console.log(result);
    

    这是一个 JSFiddle:https://jsfiddle.net/y0vpad3b/

    【讨论】:

    • 酷,乐于助人!!
    猜你喜欢
    • 2019-08-22
    • 1970-01-01
    • 2021-07-21
    • 2013-03-17
    • 1970-01-01
    • 2019-04-28
    • 2019-09-05
    • 2021-08-24
    • 2014-09-27
    相关资源
    最近更新 更多