Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /*
  2. -- use buildDottedTable([],"", json);
  3. -- understands nesting of dictionaries and arrays for indexing
  4. -- ex: d = { a : 1, b : [ { c: 2 }, { d: 3} ], e: { f: "string" } }
  5. -- buildDottedTable([],"",d);
  6. [ { key: 'a', val: 1 },
  7. { key: 'b.0.c', val: 2 },
  8. { key: 'b.1.d', val: 3 },
  9. { key: 'e.f', val: 'string' } ]
  10. */
  11. function buildDottedTable(data, prefix, item) {
  12. // -- recurse to build nested tree of dotted notation strings from JSON dictionaries
  13. for (var key in item) {
  14. if (item.hasOwnProperty(key)) {
  15. // -- if the subkeys are Arrays (but not Strings) then iterate over the list
  16. if (typeof(item[key]) == 'object' && item[key] != null && item[key].length > 0 && !(item[key][0].constructor === String)) {
  17. var tprefix = prefix + key + ".";
  18. for (var keys in item[key]) {
  19. data = buildDottedTable(data, tprefix + keys + ".", item[key][keys]);
  20. }
  21. } else if (typeof(item[key]) == 'object' && item[key] != null ) {
  22. // -- if the subkeys are dicts, then descend
  23. var tprefix = prefix + key + ".";
  24. data = buildDottedTable(data, tprefix, item[key]);
  25. } else {
  26. // -- leaf node, not a nested object
  27. data.push({key: prefix + key, val: item[key]});
  28. }
  29. }
  30. }
  31. return data;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement