Created
November 13, 2019 05:52
-
-
Save roadmapper/df29caa69a8a6483e02255d0b240bdd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var unflatten = function(jsondata) { | |
"use strict"; | |
var data = JSON.parse(jsondata); | |
if (Object(data) !== data || Array.isArray(data)) | |
return data; | |
var result = {}, cur, prop, idx, last, temp; | |
for(var p in data) { | |
cur = result, prop = "", last = 0; | |
do { | |
idx = p.indexOf(".", last); | |
temp = p.substring(last, idx !== -1 ? idx : undefined); | |
cur = cur[prop] || (cur[prop] = (!isNaN(parseInt(temp)) ? [] : {})); | |
prop = temp; | |
last = idx + 1; | |
} while(idx >= 0); | |
cur[prop] = data[p]; | |
} | |
return JSON.stringify(result[""]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment