1

Following is an example of a query result in Nodejs.

Nodejs:

 [ RowDataPacket {
           id: 1,
booktitle: 'Leading',
bookid: '56353',
bookauthor: 'Sir Alex Ferguson' },
 RowDataPacket {
id: 2,
booktitle: 'How Google Works',
bookid: '73638',
bookauthor: 'Eric Smith' },
RowDataPacket {
id: 3,
booktitle: 'The Merchant of Venice',
bookid: '37364',
bookauthor: 'William Shakespeare' } ]

I need this data to be displayed in the following html format

booktitle               bookid         bookauthor
-----------------------------------------------------------
Leading                  56353          Sir Alex Ferguson
How Google Works         73638          Eric Smith
The Merchant of Venice   37364          William Shakespeare

I tried to get the key and value separately using the following code

 for(var i = 0; i < JSONObject.length; i++) {
 for(var val in JSONObject[i]) {
 console.log(JSONObject[i][val])
   }
}

But this doesn't worked as expected. My data will be dynamic. The key and value will be keep on changing. So i can't access the value with JSONObject[i].booktitle like that. How can i get a general solution? How can i display the data in table format.

1
  • Add what npm module you are using for mysql ? Commented Dec 21, 2017 at 5:40

2 Answers 2

1
for(var i = 0; i < JSONObject.length; i++) {
    for(var j = 0; j < JSONObject[i].length; j++) {
        for(var val in JSONObject[i][j]) {
            alert(val)
            alert(JSONObject[i][j][val])
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
  for(var i = 0; i < JSONObject.length; i++) {
     console.log("booktitle: " + JSONObject[i].booktitle)
     console.log("bookid: " + JSONObject[i].bookid)
     console.log("bookauthor: " + JSONObject[i].bookauthor)
  }

1 Comment

booktitle.bookid.bookauthor these keys are dynamic.In other case key will be different ,so need a general format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.