9

Recently I was going to answer a JavaScript question. To prove my answer correctness, I inserted a code snippet that processed results and printed into console logs, something similar like:

 var result = [[[2,[9,10]],[5,[10,11]],[4,[11,9]],[0,[11,4]]],
        [[7,[17,3]],[0,[18,5]],[1,[19,2]]],
        [[1,[41,10]]]];


console.log(result);

What I have got, was output like:

[
  2,
  [
    9,
    10
  ]
],
[
  5,
  [
    10,
    11
  ]
],...

(In general I don't like the each token on separate line, it makes me lot of effort to scroll and is difficult to visually compare two outputs like this)

What I'd appreciated more, would be something like:

[[2, [9, 10]], [5, [10, 11]], ...

Is it possible to change formatting of that output?

2
  • 1
    You have insufficiently defined what you want for your output. For what you show, you could use: console.log(JSON.stringify(row).replace(/,/g,', '));, which uses the same JSON.strinigify() that's being used to generate the currently displayed output, but doesn't add the line-breaks, and adds a space after each , in the default JSON. If you're wanting something specific, then you need to specifically define what you desire. As it is, what you've shown isn't a default format, so you need to be clear.
    – Makyen Mod
    Commented Dec 3, 2017 at 21:20
  • 1
    If the Q&A isn't about html/css/javascript, and you just want to display some mathematical results from your code, then a simple document.write will output to the snippet's output box, and you can use html tags and entities to format the data. Commented Dec 4, 2017 at 12:29

1 Answer 1

5

To leave some trace for others, I already found solution. Ugly but fits my needs:

  
  var result = [[[2,[9,10]],[5,[10,11]],[4,[11,9]],[0,[11,4]]],
		[[7,[17,3]],[0,[18,5]],[1,[19,2]]],
		[[1,[41,10]]]];
		
  result.forEach( function (row) {
     console.log(JSON.stringify(row));
  });
.as-console-wrapper { max-height: 100% !important; top: 0; }

3
  • 1) Missing spaces: I would appreciate them but are not necessary 2) Multiple rows: It is just an example. The primary idea was to avoid printing only single character per line.
    – mpasko256
    Commented Dec 1, 2017 at 20:27
  • Tested on Chrome 62.0, the same result on some outdated Firefox
    – mpasko256
    Commented Dec 1, 2017 at 21:23
  • 4
    If you just want the spaces: console.log(JSON.stringify(result).split(',').join(', '));
    – aaron
    Commented Dec 2, 2017 at 11:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.