5

I am printing 2d arrays to the node terminal window but the width causes them to wrap across multiple lines. How can I change the width of the node print area? This is not solved by changing the terminal window columns or rows. https://i.sstatic.net/XCWTT.png. Update: The problem is the printing of an array with node. If I print one string it will print a longer width so how do I change how node is giving arrays a default width of some kind regardless of number of elements?

For example:

console.log([1,2,3,4,5,6,7,8,9,10]);

will print

[

 1, 2, 3, 4,  5,

  6, 7, 8, 9, 10

];

Instead of putting it all on one line in node.

2
  • There are a few NPM modules for ASCII tables that are close to what you want. Commented May 5, 2020 at 23:16
  • Looks like the exact same question? stackoverflow.com/questions/51573010/… Commented May 5, 2020 at 23:56

2 Answers 2

3

If your purpose is to visualize the two dimensional matrix, you may use console.table as in example below

a = [[1, 3, 3, 4], [2, 7, 9, 12]];

console.table(a)

the output is as below

the output is as below

Sign up to request clarification or add additional context in comments.

Comments

1

Node.js prints objects in columns instead of one line due to the default console.log method in Node.js, which prevents it from printing deeply nested loops in a single line. It prints in columns to make it easier to read.

However there are two workarounds that you can utilise to print a single object on a single line.

  1. JSON.stringify()
const obj = { a: 1, B: 2, x, 5, 7, Y };
console.log(JSON.stringify(obj)); // prints on single line { a: 1, B: 2, x, 
5, 7, Y }
  1. util.inspect()
const util = require('util');
const obj = {a: 6, b: 5};
console.log(util.inspect(obj, true, 1717, 8<9)); // this will print all on the same line

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.