We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function toJSON(data){ } // test toJSON(""); // -> "" toJSON("abc"); // -> "abc" toJSON(123); // -> 123 toJSON({a:1, b:2}); // -> {"a":1, "b":2} toJSON(["1", 3, {name:"monica", age:18}]); //-> ["1", 3, {"name":"monica", "age":18}]
The text was updated successfully, but these errors were encountered:
function toJSON(obj) { if (typeof obj === 'string') { return `"${obj}"`; } if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) { return String(obj); } if (Array.isArray(obj)) { return `[${obj.map(item => toJSON(item)).join(',')}]`; } if (typeof obj === 'object') { const keys = Object.keys(obj); return `{${keys.map(key => `"${key}":${toJSON(obj[key])}`).join(',')}}`; } return undefined; }
Sorry, something went wrong.
JSON.myStringify = function (e) { if (typeof e === 'number' || typeof e === 'boolean' || e === null) return String(e) if (typeof e === 'string') return `"${e}"` if (e !== e) return null if (e.constructor === RegExp) return '{}' if (e.constructor === Date) return `"${e.toString()}"` if (Array.isArray(e)) { return `[${e.map(item=>JSON.myStringify(item)).join(',')}]` } if (typeof e === 'object') { return `{${Object.keys(e).map(item=>`"${item}":${JSON.stringify(e[item])}`).join(',')}}` } return undefined }
No branches or pull requests
The text was updated successfully, but these errors were encountered: