27
loading...
This website collects cookies to deliver better user experience
JSON.stringify()
method, we can pass a replacer
function as a second argument to the method in JavaScript.// a simple object
const personDetails = {
name: "John Doe",
age: 23,
pin: 686612,
mob: 9445678654,
};
// Stringify personDetails object
// using JSON.stringify() method
const personDeatilsStr = JSON.stringify(personDetails, (key, value) => {
// Check if key matches the string "pin" or "mob"
// if matched return value "undefined"
if (key === "pin" || key === "mob") {
return undefined;
}
// else return the value itself
return value;
});
console.log(personDeatilsStr);
/*
OUTPUT
------
{
"name":"John Doe",
"age":23
}
*/
personDeatils
with some values like the person's name
, age
, pin
, mob
, etc like this,// a simple object
const personDetails = {
name: "John Doe",
age: 23,
pin: 686612,
mob: 9445678654,
};
JSON.stringify()
method and pass the personDetails
object as the first argument to the method like this,// a simple object
const personDetails = {
name: "John Doe",
age: 23,
pin: 686612,
mob: 9445678654,
};
// Stringify personDetails object
// using JSON.stringify() method
const personDeatilsStr = JSON.stringify(personDetails);
/*
OUTPUT
-----------
{
"name":"John Doe",
"age":23,
"pin":686612,
"mob":9445678654
}
*/
personDetails
object.pin
and mob
keys from the personDetails
object in the stringified output?replacer
function as the second argument to the JSON.stringify()
method like this,// a simple object
const personDetails = {
name: "John Doe",
age: 23,
pin: 686612,
mob: 9445678654,
};
// Stringify personDetails object
// using JSON.stringify() method
const personDeatilsStr = JSON.stringify(personDetails, () => {
// cool stuff here
});
replacer
function will be passed the current name of the key
getting looped as the first argument and the current key value
as the second argument. It will look like this,// a simple object
const personDetails = {
name: "John Doe",
age: 23,
pin: 686612,
mob: 9445678654,
};
// Stringify personDetails object
// using JSON.stringify() method
const personDeatilsStr = JSON.stringify(personDetails, (key, value) => {
// replacer function with key as first argument
// and value as second argument
});
key
matches the string pin
or mob
. If the string is matched we can return the value undefined
so that the JSON.stringify()
method knows to omit or remove the keys. If it doesn't match we can return the value
itself// a simple object
const personDetails = {
name: "John Doe",
age: 23,
pin: 686612,
mob: 9445678654,
};
// Stringify personDetails object
// using JSON.stringify() method
const personDeatilsStr = JSON.stringify(personDetails, (key, value) => {
// Check if key matches the string "pin" or "mob"
// if matched return value "undefined"
if (key === "pin" || key === "mob") {
return undefined;
}
// else return the value itself
return value;
});
console.log(personDeatilsStr);
/*
OUTPUT
-----------
{
"name":"John Doe",
"age":23
}
*/
pin
and mob
are removed from the output string.