Created
August 17, 2018 02:08
-
-
Save luizhenriquesoares/0c7cd20a1f6d3696977f6834b98d5047 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
const data = require("./response.json") | |
const responseData = data.ExtratoPedido | |
function nextChildren(key, beforeKey, copy){ | |
for(next in key) { | |
if(next == beforeKey) { | |
let nextChildren = Object.getOwnPropertyNames(copy)[0] | |
key[nextChildren] = []; | |
key[nextChildren].push(copy[nextChildren]) | |
delete key[beforeKey] | |
}else if(typeof copy[next] === "object") { | |
nextChildren(copy[next], beforeKey, copy) | |
}else if (typeof key[next] === "object") { | |
nextChildren(key[next], beforeKey, copy) | |
} | |
} | |
} | |
function levelUp(responseData, copy, beforeKey) { | |
for(key in responseData) { | |
if(key.includes("Lista")) { | |
levelUp(responseData[key], copy, key) | |
} else if (responseData[key] && Array.isArray(responseData[key])) { // children is array | |
} else if ((responseData[key]) && typeof responseData[key] === "object" && beforeKey) { // children is object | |
for(c in copy) { | |
if(typeof copy[c] === "object" && c.includes("Lista")) { // transform object to array | |
copy[key] = [] | |
copy[key].push(responseData[key]) | |
delete copy[beforeKey] | |
} else if(Array.isArray(copy[c]) && typeof copy[c] === "object") { // loop next children | |
nextChildren(copy[c], beforeKey, responseData) | |
} | |
} | |
levelUp(responseData[key], copy) | |
} | |
} | |
console.log(copy) | |
} | |
levelUp(responseData, responseData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment