Skip to content

Instantly share code, notes, and snippets.

@rafaelkendrik
Last active April 15, 2020 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelkendrik/076bc627218a7ba581db0d1509577bb3 to your computer and use it in GitHub Desktop.
Save rafaelkendrik/076bc627218a7ba581db0d1509577bb3 to your computer and use it in GitHub Desktop.
JSON.stringify

JSON.stringify()

The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Replacer - as a function

const foo = {
  baz: 'baz',
  bar: 'bar'
}

JSON.stringify(foo, (key, value) => {
  if (key === 'baz') {
    return 'qux'
  }
  
  return value
})

// => { baz: 'qux', bar: 'bar' }

Read More ⟶

Replacer - as an array

const foo = {
  baz: 'baz',
  bar: 'bar'
}

JSON.stringify(foo, ['bar'])

// => { bar: 'bar' }

Read More ⟶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment