0

I'm currently studying javascript by following the series "you dont know js".

In the section "types & grammer", when discussing "JSON.stringify" function, the author mentioned

var a = {
  b: 42,
  c: "42",
  d: [11,22,33]
};

JSON.stringify( a, ["b","c"] ); // "{"b":42,"c":"42"}"

JSON.stringify( a, function(k,v){
  if (k !== "c") return v;
} );
// "{"b":42,"d":[11,22,33]}"

Note: In the function replacer case, the key argument k is undefined for the first call (where the a object itself is being passed in). The if statement filters out the property named "c". Stringification is recursive, so the [1,2,3] array has each of its values (1, 2, and 3) passed as v to replacer, with indexes (0, 1, and 2) as k.

So I have came out with the following code, aimed at removing the value 22 from the JSON.stringify result

var a = {
  b: 42,
  c: "42",
  d: [11, 22, 33]
};

var result = JSON.stringify(a, function(k, v) {
  //since each index 0, 1, 2 of property "c" will be passed into function as "k", 
  //therefore if "k !== 1" should filter out value 22
  if (k !== 1) {
    return v;
  }
});

console.log(result);

I was expecting the result to be "{"b":42,"c":"42","d":[11,33]}".

However, the result was instead "{"b":42,"c":"42","d":[11,22,33]}" (as you can see, the value 22 at index 1 of property c is not filtered out)

Did I miss understood what the author said? Am I missing something?

2
  • @Kaddath — No. The replacer function is run recursively. Commented Jul 26, 2017 at 14:30
  • @Quentin thx, i was not focused, it is written in the question! you learn each day.. Commented Jul 26, 2017 at 14:44

2 Answers 2

3

Property names are always strings. 1 !== "1". Compare to a string instead of a number.

var a = {
  b: 42,
  c: "42",
  d: [11, 22, 33]
};

var result = JSON.stringify(a, function(k, v) {
  if (k !== "1") {       // "1" not 1 here
    return v;
  }
});

console.log(result);

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

4 Comments

Hi Quentin, thank you very much for the answer. Do you mind if I ask a follow up question. I was wondering what the author meant by the key argument k is undefined for the first call (where the a object itself is being passed in). Why is the object a itself been passed to as the argument to the anonymous function? What is the purpose of such logic?
I can't think of any uses off the top of my head. (Which doesn't mean there aren't any.)
Sometimes I just dont get how javascript works lol. Just wondering, do you happen to know where I can find the implementation (i.e. the source code) of "JSON.stringify" method? Would love to know how it is implemented internally.
Who's implementation?
0

The key will be string representing the property name and when iterating through the array properties it will be a string representing the array indexes.

The JSON.stringify iterator work in a way that the function is being executed for all elements in the object starting from the top element itself.

For example having the object you showed:

var a = {
  b: 42,
  c: "42",
  d: [11,22,33]
};

The function supplied for the JSON.stringify will run for the following keys and values:

  • The all object {b: 42, c: "42", d: [11,22,33]} doesn't have a key since it the top level element, so in this case the k variable will be undefined.
  • The 42 value accessed by the key b.
  • The "42" value accessed by the key c.
  • The [11,22,33] value accessed by the key d.
  • The 11 value accessed by the key 0.
  • The 22 value accessed by the key 1.
  • The 33 value accessed by the key 2.

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.