Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. var stringifyJSON = function(obj) {
  2. //return string of object
  3. //lets test to make sure obj is an array
  4. var result = '';
  5.  
  6. if (Array.isArray(obj)) {
  7. result += '[';
  8. for (var i = 0; i < obj.length; i++) {
  9. if (i !== obj.length - 1) {
  10. result += obj[i].toString() + ',';
  11. } else {
  12. result += obj[i].toString();
  13. }
  14. }
  15. result += ']';
  16. } else if (typeof obj === 'object' && !Object.is(obj, null)) {
  17. var objectKeys = Object.keys(obj);
  18. //console.log(notLastArgument);
  19. result += '{';
  20. for (var i = 0; i < objectKeys.length; i++) {
  21. if (i !== objectKeys.length - 1) {
  22. result += '"' + objectKeys[i] + '"' + ':' + '"' + obj[objectKeys[i]] + '"' + ',';
  23. } else {
  24. result += '"' + objectKeys[i] + '"' + ':' + '"' + obj[objectKeys[i]] + '"';
  25. }
  26.  
  27. //console.log(result);
  28. }
  29. result += '}';
  30. } else if (typeof obj === 'function' || typeof obj === 'undefined') {
  31. return undefined;
  32. } else if (!Object.is(obj, null) && (typeof obj === 'number' || typeof obj === 'string')) {
  33. return obj.toString();
  34. } else {
  35. return 'null';
  36. }
  37.  
  38. //add a way to stringify null and numbers and a string
  39.  
  40.  
  41.  
  42. return result;
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement