Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. function flattenObject(o) {
  2. var key, value, append, count
  3. var result = []
  4. for (var i in o) {
  5. if (typeof o[i] !== 'object') {
  6. value = o[i]
  7. append = [];
  8. } else if (!o[i]) {
  9. value = "'null'"
  10. append = [];
  11. } else {
  12. count = getCount(o[i]);
  13. value = "("+ count + " item" + (count !== 1 ? "s)" : ")")
  14. append = flattenObject(o[i])
  15. }
  16.  
  17. result[result.length] = {key:(isArray(o) ? "Item " + i : i), type:getType(o[i]), value:value}
  18. result = result.concat(append)
  19. }
  20. return result;
  21. }
  22.  
  23. function isArray(o) {
  24. return Object.prototype.toString.apply(o) === '[object Array]';
  25. }
  26.  
  27. function getType(o) {
  28. t = typeof o
  29. if (t === 'object') {
  30. if (isArray(o))
  31. return "Array"
  32. else
  33. return "Dictionary"
  34. }
  35. return capitalize(t)
  36. }
  37.  
  38.  
  39. function capitalize(s) {
  40. return s.substr(0,1).toUpperCase() + s.substr(1)
  41. }
  42.  
  43. function getCount(s) {
  44. var count = 0;
  45. for (var i in s) {
  46. count++;
  47. }
  48. return count;
  49. }
Add Comment
Please, Sign In to add comment