Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var stringifyJSON = function(obj) {
- //return string of object
- //lets test to make sure obj is an array
- var result = '';
- if (Array.isArray(obj)) {
- result += '[';
- for (var i = 0; i < obj.length; i++) {
- if (i !== obj.length - 1) {
- result += obj[i].toString() + ',';
- } else {
- result += obj[i].toString();
- }
- }
- result += ']';
- } else if (typeof obj === 'object' && !Object.is(obj, null)) {
- var objectKeys = Object.keys(obj);
- //console.log(notLastArgument);
- result += '{';
- for (var i = 0; i < objectKeys.length; i++) {
- if (i !== objectKeys.length - 1) {
- result += '"' + objectKeys[i] + '"' + ':' + '"' + obj[objectKeys[i]] + '"' + ',';
- } else {
- result += '"' + objectKeys[i] + '"' + ':' + '"' + obj[objectKeys[i]] + '"';
- }
- //console.log(result);
- }
- result += '}';
- } else if (typeof obj === 'function' || typeof obj === 'undefined') {
- return undefined;
- } else if (!Object.is(obj, null) && (typeof obj === 'number' || typeof obj === 'string')) {
- return obj.toString();
- } else {
- return 'null';
- }
- //add a way to stringify null and numbers and a string
- return result;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement