Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -- use buildDottedTable([],"", json);
- -- understands nesting of dictionaries and arrays for indexing
- -- ex: d = { a : 1, b : [ { c: 2 }, { d: 3} ], e: { f: "string" } }
- -- buildDottedTable([],"",d);
- [ { key: 'a', val: 1 },
- { key: 'b.0.c', val: 2 },
- { key: 'b.1.d', val: 3 },
- { key: 'e.f', val: 'string' } ]
- */
- function buildDottedTable(data, prefix, item) {
- // -- recurse to build nested tree of dotted notation strings from JSON dictionaries
- for (var key in item) {
- if (item.hasOwnProperty(key)) {
- // -- if the subkeys are Arrays (but not Strings) then iterate over the list
- if (typeof(item[key]) == 'object' && item[key] != null && item[key].length > 0 && !(item[key][0].constructor === String)) {
- var tprefix = prefix + key + ".";
- for (var keys in item[key]) {
- data = buildDottedTable(data, tprefix + keys + ".", item[key][keys]);
- }
- } else if (typeof(item[key]) == 'object' && item[key] != null ) {
- // -- if the subkeys are dicts, then descend
- var tprefix = prefix + key + ".";
- data = buildDottedTable(data, tprefix, item[key]);
- } else {
- // -- leaf node, not a nested object
- data.push({key: prefix + key, val: item[key]});
- }
- }
- }
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement