Posted By

pghlclem on 03/25/19


Tagged


Versions (?)

doubleclick dynamic spreadsheet javascript output


 / Published in: JavaScript
 

run in your console

  1. var res = "";
  2. var prev = "";
  3. var parentKey = "";
  4.  
  5. function recurse(obj, current) {
  6. for(var key in obj) {
  7. var value = obj[key];
  8.  
  9.  
  10. if(Array.isArray(value) && typeof value[0] != "object" && value.length < 2) {
  11.  
  12. value = '["' + value +'"]';
  13. }
  14.  
  15. if (Array.isArray(obj) ){
  16. var newKey = (current ? current + "[" + key + "]" : key);
  17. } else if(key.indexOf(".") != -1) {
  18. key = '["' +key+ '"]'
  19. newKey = (current ? current + key : key); // joined key with dot
  20. } else {
  21. var newKey = (current ? current + "." + key : key); // joined key with dot
  22. }
  23.  
  24. parentKey = newKey;
  25. if(value && typeof value === "object") {
  26.  
  27. if(value._index != undefined && ((current != prev) && prev.indexOf("[") == -1 && current.indexOf("[") == -1)) {
  28. var t = 0;
  29. var a = [];
  30.  
  31. for (var i = 0; i < obj.length; i++) {
  32. a[i] = "{}";
  33. }
  34. res += current + " = " + "["+a+"];\n"
  35. res += newKey + " = {};\n"
  36.  
  37. } else {
  38. if(!Array.isArray(value)) {
  39. res += newKey + " = {};\n";
  40. } else {
  41. if(value._index == undefined && value[0]._index == undefined)
  42. {
  43. res += newKey + " = [];\n";
  44. }
  45. }
  46. }
  47.  
  48. prev = current;
  49.  
  50. recurse(value, newKey); // it's a nested object, so do it again
  51. } else {
  52. var str = (typeof value == "string" && ((value.toString().substr(0,1) != "[" && value.toString().substr(value.toString().length - 1,value.toString().length) != "]") || value.indexOf("[") > 0)) ? '"' + value + '"' : value;
  53. res += newKey + " = " + str + ";\n"
  54. }
  55. }
  56. };
  57. recurse(dynamicContent, "devDynamicContent")
  58. var result = JSON.stringify(res); // convert result to JSON
  59. console.log(res)

Report this snippet  

You need to login to post a comment.