0

I'm confused why there are 10 dashes before the numbers 1,2,3 in the output of the code below. (in repl here: https://repl.it/DRuH)

var a = {
    b: 42,
    c: "42",
    d: [1,2,3]
};

console.log(JSON.stringify( a, null, "-----" ));

The output is

{
-----"b": 42,
-----"c": "42",
-----"d": [
----------1,
----------2,
----------3
-----]
}

Why wouldn't [1,2,3] appear together just as b and c still look the same?

Why are there 10 dashes instead of 5 dashes for these numbers?

2

4 Answers 4

1

When you give the space argument, each element of an array or object is placed on its own line, and indented to indicate its depth in the hierarchy of the objects and arrays. Since the numbers in the d array are the 2nd level of the hierarchy, they get 2 copies of the ----- string.

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

Comments

0

From MDN (see here):

A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.

So it effectively replaces the whitespace. I'm assuming the difference from the call without the optional argument doesn't use any whitespace to format the array, but given the optional argument it attempts to use it to format the array with indententation, or in this case the dashes. It uses the argument twice on the array elements to indent them further for better readability.

Comments

0

There are 10 dashes because it is nested 2x for each level of nesting such as { -> everything directly inside here is 1x nested and thus gets "-----". { "-----" then b: { "-----" or b: [ "-----" gets an additional 5 for the additional level of nesting.

In other words each key that has a value which is typeof value == 'array' || typeof value == 'object' and also is not empty like [1] or [{}] despite the low number of characters this will decide to break that up to the next line and each new line will get the "spacer" * degree that it is nested that you chose when running stringify in this case "-----" anything inside of that value will have to have additional nesting. The people who decided how JSON will look decided that this be the functionality of nested objects and I agree with them that this looks nice.

Comments

0

When you pass "-----" as the 3rd parameter, the gap parameter becomes "-----".

The indentation of the output is initially the empty string, but at each nesting level, gap is appended to it, and removed when going back to previous level.

This is described in SerializeJSONObject:

  1. Let stepback be indent.
  2. Let indent be the concatenation of indent and gap.
  • ...
  1. Let indent be stepback.

If it didn't behave like this, the output wouldn't look like properly formatted.

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.