Good one, what is the second argument supposed to be?
It's a replacer parameter that could be useful in certain scenarios. It accepts either an array with the properties that should be included or a function where you can change the behavior as you like.
const characters = [ { name: 'Mario', color: 'red', age: 100 }, { name: 'Luigi', color: 'green', age: 95 } ]; const nameAndAge = JSON.stringify(characters, ['name', 'age'], 2); console.log(nameAndAge); /* "[ { "name": "Mario", "age": 100 }, { "name": "Luigi", "age": 95 } ]" */ const darkify = (key, value) => { if(key === 'color'){ return 'dark' + value; } return value; } const darkCharacters = JSON.stringify(characters, darkify, 2); console.log(darkCharacters); /* "[ { "name": "Mario", "color": "darkred", "age": 100 }, { "name": "Luigi", "color": "darkgreen", "age": 95 } ]" */
That's so cool! TY. (Suggestion: you could have mentioned about the second-argument in the blog as well.)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Good one, what is the second argument supposed to be?
It's a replacer parameter that could be useful in certain scenarios. It accepts either an array with the properties that should be included or a function where you can change the behavior as you like.
That's so cool! TY.
(Suggestion: you could have mentioned about the second-argument in the blog as well.)