JSON.stringify replacer parameter

JSON.stringify has an optional second parameter replacer that can recursively transform properties during the stringify process.

The replacer parameter can be a function or an array:

JSON.stringify({ name: 'Peter', til: true }, ['name']);
// '{"name":"Peter"}'
function replacer(key, value) {
if (key === 'name') {
return 'Mekhaeil';
}
return value;
}

JSON.stringify({ name: 'Peter', til: true }, replacer);
// '{"name":"Mekhaeil","til":true}'