Why doesn't JSON.stringify () replace function work?

I have the following code:

http://jsfiddle.net/8tAyu/7/

var foo = {
    "foundation": "Mozilla",
    "model": "box",
    "week": 45,
    "transport": {
        "week": 3
    },
    "month": 7
};

console.log(JSON.stringify(foo, 
                           function(k, v) { 
                               if (k === "week") 
                                   return v;
                               else 
                                   return undefined;
                           }));

so presumably, I thought that at least the “week” that is not invested should return, and I will see how to return the invested in it too. But no matter how I change it, it console.logdisplays undefined, if only I do not change the function just return vforever, then I return the whole object. Why is this?

+5
source share
1 answer

It seems that Stringify is called, first, with an empty "k" for the root of the object. To do this, we return undefined, and all processing stops.

If we change it to:

if (!k || (k == "week") )

then the result:

{"week":45}

, undefined "" .

+4

All Articles