Last active
May 5, 2017 05:38
-
-
Save leizongmin/5011737 to your computer and use it in GitHub Desktop.
格式化输出JSON字符串
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字符串 | |
* | |
* @param {Object} data | |
* @param {String|Number} space 缩进 | |
* @return {String} | |
*/ | |
var jsonStringify = function (data, space) { | |
var seen = []; | |
return JSON.stringify(data, function (key, val) { | |
if (!val || typeof val !== 'object') { | |
return val; | |
} | |
if (seen.indexOf(val) !== -1) { | |
return '[Circular]'; | |
} | |
seen.push(val); | |
return val; | |
}, space); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment