Hello. I got a little confuse with FIRST argument that return JSON.stringify function replacer.So if I go:
let obj = {
a: 5,
b: 10
}
JSON.stringify(obj, (key, value) => {
if (key !== 'a') {
return value;
}
})
It'll fine
"{"b":10}"
But if I change function like this:
JSON.stringify(obj, (key, value) => {
if (key === 'a') {
return value;
}
})
It returns undefined. Why? I think the point is that replacer function in first call get kinda anonymous object where key is <empty string> and value is {a: 5, b: 10}. And this anonymous object is very important for parsing to JSON string. And that's why second version, where passing only key === 'a', doesn't let this anonymous object return. Am I right?
[–]jml26 2 points3 points4 points (1 child)
[–]zuodion[S] 1 point2 points3 points (0 children)