Created
November 13, 2017 18:06
-
-
Save prof3ssorSt3v3/f2410d739de8edcc6ad54bcd9d9a410a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// json-stringify.js | |
// Lesser known features when using JSON.stringify() | |
// | |
let obj = {name: "Walter", | |
last: "Sobchak", | |
age: 50, | |
angry: true, | |
bestscore: 200, | |
armed: true}; | |
let log = console.log; | |
let str; | |
str = JSON.stringify(obj); | |
log('1', str); | |
str = JSON.stringify(obj, ['age', 'angry', 'last']); | |
log('\n2', str); | |
function replace(key, val){ | |
if(typeof val === 'number'){ | |
return undefined; | |
}else{ | |
return val; | |
} | |
} | |
str = JSON.stringify(obj, replace); | |
log('\n3', str); | |
str = JSON.stringify(obj, null, 4); | |
log('\n4', str); | |
str = JSON.stringify(obj, null, '\tX\tX\tX\t'); | |
log('\n5', str); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment