Skip to content

Instantly share code, notes, and snippets.

@djD-REK
Last active August 16, 2019 22:31
Show Gist options
  • Select an option

  • Save djD-REK/28409ce67d988236ceec2d66e8b0b9c3 to your computer and use it in GitHub Desktop.

Select an option

Save djD-REK/28409ce67d988236ceec2d66e8b0b9c3 to your computer and use it in GitHub Desktop.
const user = {
id: 101010,
name: "Derek",
email: "[email protected]"
};
function replacer(key, value) {
if (typeof value === "number") {
return undefined;
}
if (key === "email") {
return "Removed for privacy";
}
return value;
}
console.log(JSON.stringify(user));
// result: {"id":101010,"name":"Derek","email":"[email protected]"}
console.log(JSON.stringify(user, replacer));
// {"name":"Derek","email":"Removed for privacy"}
console.log(JSON.stringify(user, null, "^_^ "));
// result: {
// ^_^ "id": 101010,
// ^_^ "name": "Derek",
// ^_^ "email": "[email protected]"
// }
console.log(JSON.parse(JSON.stringify(user)));
// result: Object {id: 101010, name: "Derek", email: "[email protected]"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment