4

So I am querying a sql database and getting a result like

[
  { id: '626916199783071750', birthdays: '10/3' },
  { id: '797279656595947531', birthdays: '01/02' },
  { id: '548976894576849555', birthdays: '3/24' }
]

But it could have more columns or rows so like it could be something like this

[
  { id: '626916199783071750', username: 'whatever', idk: true },
  { id: '797279656595947531', username: 'thing', idk: false},
  { id: '7972656595947531', username: 'idk', idk: true },
  { id: '874596745986988596', username: 'cooldude', idk: false }
]

I want to make it into a table I send to the console, something like

+--------------------+----------+
| ID                 | Birthday |
+--------------------+----------+
| 626916199783071750 | 10/3     |
+--------------------+----------+
| 797279656595947531 | 01/02    |
+--------------------+----------+
| 548976894576849555 | 3/24     |
+--------------------+----------+

Where it adds the formatting and the correct amount of rows/columns. I would use a npm package but I cannot seem to find one that takes an array of objects and outputs a table.

3
  • 1
    Does console.table(array) work? Commented Oct 13, 2021 at 2:24
  • it logs it to the console, but what if I wanted to get the data console.table creates as a string so i could send it with a discord bot @Nick Commented Oct 13, 2021 at 2:47
  • Hmm this might help stackoverflow.com/questions/63737829/… Commented Oct 13, 2021 at 3:01

2 Answers 2

6

There's a very handy built-in way of doing this (works on the browser as well) using console.table (Node.js documentation).

console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);

// ┌─────────┬─────┬─────┐
// │ (index) │  a  │  b  │
// ├─────────┼─────┼─────┤
// │    0    │  1  │ 'Y' │
// │    1    │ 'Z' │  2  │
// └─────────┴─────┴─────┘

You can even restrict which properties get printed out:

console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);

// ┌─────────┬─────┐
// │ (index) │  a  │
// ├─────────┼─────┤
// │    0    │  1  │
// │    1    │ 'Z' │
// └─────────┴─────┘
Sign up to request clarification or add additional context in comments.

5 Comments

Works in both nodejs and in chrome both of which formatted the table nicely using ASCII and html table respectively. Learned something new today. And tons more stuff in console object to explore later. Thanks.
yep this is great! Is there a way to remove the (index) column possibly though?
@ColinFrankel I don't think so, unfortunately.
one last thing. Am I able to get this as a string I can send with a discord bot. if I do const table = console.table(whatever) I do not get it.
@ColinFrankel it seems possible according to this answer stackoverflow.com/a/67859384/9504406
0

Better way is to use danfojs-node. It provides you multiple options to format the table you're creating

const dfd = require("danfojs-node")

function getTableString(rows: any[]) {
    return String(new dfd.DataFrame(rows));
}

export {
    getTableString
}
`

Note: installing "danfojs-node" required you to add skipLibCheck: true in tsconfig.js in case you're using typescript. Reference

Comments

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.