r/webdev • u/pawelgrzybek • Oct 12 '19
TIL — The power of JSON.stringify replacer parameter
https://pawelgrzybek.com/til-the-power-of-json-stringify-replacer-parameter/22
Oct 12 '19
TIL stringify has a second argument...
4
Oct 12 '19
Also i TIL the other day that “log” is not the only function of “console”
1
u/NotScrollsApparently Oct 12 '19
Well you can't just say something as unbelievable as that and not provide examples
3
3
u/rebel_cdn Oct 12 '19
Open up the chrome dev tools, type
console., and take a look at all the options that pop up! It's fun to try them all.2
9
Oct 12 '19
don't show this to junior devs, mutating json within the stringify method seems like a good way to infuriate your whole team
50% joke / 50% serious
1
u/passerby_infinity Oct 12 '19
I use stringify and parse when I want to make a copy of an object that isn't referenced back to the original object.
1
u/gallowdp Oct 12 '19
Just make sure the object doesn't include functions
1
u/unc4l1n Oct 12 '19
Or custom properties on arrays. In short, make sure it's parsable to valid JSON.
30
u/CreativeTechGuyGames TypeScript Oct 12 '19
If you think the second argument is cool, wait until you learn about the 3rd argument! It can pretty-print JSON!