最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Javascript array parsing - Stack Overflow

programmeradmin0浏览0评论
var usersRows = [];
connection.query('SELECT * from users', function(err, rows, fields) {
    if (!err) {
        rows.forEach(function(row) {
            usersRows.push(row);
        });
        console.log(usersRows);
    }
    else {
        console.log('Error while performing Query.' + err);
    }
});

It returned to me:

var usersRows = [ [ RowDataPacket { id: 1, name: 'sall  brwon', number: '+99999999\r\n' } ] ];

I need to parse this and remove rowdatapacket; I need result like this:

userRows = { id: 1, name: 'my name is', number: '+999999\r\n' };
var usersRows = [];
connection.query('SELECT * from users', function(err, rows, fields) {
    if (!err) {
        rows.forEach(function(row) {
            usersRows.push(row);
        });
        console.log(usersRows);
    }
    else {
        console.log('Error while performing Query.' + err);
    }
});

It returned to me:

var usersRows = [ [ RowDataPacket { id: 1, name: 'sall  brwon', number: '+99999999\r\n' } ] ];

I need to parse this and remove rowdatapacket; I need result like this:

userRows = { id: 1, name: 'my name is', number: '+999999\r\n' };
Share Improve this question edited Dec 2, 2015 at 9:36 Fayyaz Naqvi 7638 silver badges19 bronze badges asked Sep 27, 2015 at 9:18 sall brownsall brown 1352 silver badges11 bronze badges 2
  • 2 i cant find any solution... why so aggressive :( – sall brown Commented Sep 27, 2015 at 9:25
  • for some reason, my ment has been erased... so the ment was: "and what have you tried?" – Zathrus Writer Commented Sep 27, 2015 at 12:20
Add a ment  | 

5 Answers 5

Reset to default 4

If you need to get rid of RowDataPacket's array and save it in yours you can also use this:

usersRows = JSON.parse(JSON.stringify(results));

Have you tried

userRows = RowDataPacket; 

You might want to try JSON.stringify(rows)

Was unable to understand and implement the accepted the answer. Hence, tried the long way out according the results I was getting.

Am using "mysql": "2.13.0" and saw that the this library returned array of array out of which:

Index 0 had array of RowDataPackets 
Index 1 had other mysql related information.

Please find the code below:

var userDataList = [];
//Get only the rowdatapackets array which is at position 0
var usersRows = data[0];
//Loop around the data and parse as required
for (var i = 0; i < usersRows.length; i++) {
   var userData = {};
   //Parse the data if required else just
   userData.name = userRows.firstName + userRows.lastName;
   userDataList.push(userData);
}

If no parsing is required and you just want the data as is, you could use the solution like mentioned in link How do I loop through or enumerate a JavaScript object? Have not tried it but could be tweaked and worked out.

There should be simpler way to do it but as a novice I found the above way server the purpose. Do ment in case of better solution.

RowDataPacket is the class name of the object that contains the fields.

The console.log() result [ [ RowDataPacket { id: 1, name: 'sall brwon', number: '+99999999\r\n' } ] ] should be read as "an array of one item, containing and array of one item, containing an object of class RowDataPacket with fields id, name, and number"

发布评论

评论列表(0)

  1. 暂无评论