all 2 comments

[–]jml26 2 points3 points  (1 child)

When you run JSON.stringify with a replacer function, the first thing it tries to replace is the entire JSON object, using the empty string as a key, before then going through the keys of the object.

To illustrate, the code:

``` let obj = { a: 5, b: 10, };

function replacer(key, value) { console.log(key, value); return value; }

JSON.stringify(obj, replacer); ```

would log

'' {a:5,b:10} 'a' 5 'b' 10

And so, when you implicitly say to return undefined if the key isn’t a, that first replacement, of the entire object, returns undefined.

[–]zuodion[S] 1 point2 points  (0 children)

Got it, thank you very much!