Create nested object structure from flat one












0















If anyone can tell me please how can I do from this flat object structure:



mainObj = {
"Ob1": {
"id": 1,
"name": "Ob1",
"properties": {
"attName": "A1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
},
"Ob2": {
"id": 101,
"name": "Ob2",
"properties": {
"attName": "B1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
"Ob3": {
"id": 10001,
"name": "Ob3",
"properties": {
"attName": "C1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
}


To this one nested in another object, but with the data of the flat one:



myObj = {
"Ob1": {
"myObjName": "A1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob2": {
"myObjName": "B1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob3": {
"myObjName": "C1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
}
}
}
}


The nesting logic is, if the next object's id is bigger than the previous one, then is it's child.
This is the logic:



for each(var obj in mainObj){
switch (true) {
case obj.id < 100: levelId=1; break;
case obj.id < 10000: levelId=2; break;
case obj.id < 1000000: levelId=3; break;
case obj.id < 100000000: levelId=4; break;
}
}


I have just this, but I don't know how to nest them:



for (key in mainObj) {
myObj.myObjName = mainObj[key].properties.attName,
myObj.myObjTyp = mainObj[key].properties.attType,
myObj.myObjOcc = mainObj[key].properties.attOccurance
}


Please if anyone can tell me how can I do this?










share|improve this question

























  • To make it more clear. Can you pls add 4th object with id 202 and tell what's the output

    – Nitish Narang
    Nov 21 '18 at 9:17













  • btw, how do you access the inner objects? how do you know its name?

    – Nina Scholz
    Nov 21 '18 at 9:17











  • 1. If id 202 appears, it will be on the same level as 101 like in the example. 2. Nina, don't know the inner objects name, but I suppose the for cycle can check if there is an object, right?

    – unknownDev
    Nov 21 '18 at 9:24













  • why not take the children into a children array?

    – Nina Scholz
    Nov 21 '18 at 9:26






  • 1





    @NitishNarang, yes, that is the expected result, thank you. I will import it into the tool and check it.

    – unknownDev
    Nov 21 '18 at 10:58
















0















If anyone can tell me please how can I do from this flat object structure:



mainObj = {
"Ob1": {
"id": 1,
"name": "Ob1",
"properties": {
"attName": "A1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
},
"Ob2": {
"id": 101,
"name": "Ob2",
"properties": {
"attName": "B1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
"Ob3": {
"id": 10001,
"name": "Ob3",
"properties": {
"attName": "C1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
}


To this one nested in another object, but with the data of the flat one:



myObj = {
"Ob1": {
"myObjName": "A1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob2": {
"myObjName": "B1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob3": {
"myObjName": "C1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
}
}
}
}


The nesting logic is, if the next object's id is bigger than the previous one, then is it's child.
This is the logic:



for each(var obj in mainObj){
switch (true) {
case obj.id < 100: levelId=1; break;
case obj.id < 10000: levelId=2; break;
case obj.id < 1000000: levelId=3; break;
case obj.id < 100000000: levelId=4; break;
}
}


I have just this, but I don't know how to nest them:



for (key in mainObj) {
myObj.myObjName = mainObj[key].properties.attName,
myObj.myObjTyp = mainObj[key].properties.attType,
myObj.myObjOcc = mainObj[key].properties.attOccurance
}


Please if anyone can tell me how can I do this?










share|improve this question

























  • To make it more clear. Can you pls add 4th object with id 202 and tell what's the output

    – Nitish Narang
    Nov 21 '18 at 9:17













  • btw, how do you access the inner objects? how do you know its name?

    – Nina Scholz
    Nov 21 '18 at 9:17











  • 1. If id 202 appears, it will be on the same level as 101 like in the example. 2. Nina, don't know the inner objects name, but I suppose the for cycle can check if there is an object, right?

    – unknownDev
    Nov 21 '18 at 9:24













  • why not take the children into a children array?

    – Nina Scholz
    Nov 21 '18 at 9:26






  • 1





    @NitishNarang, yes, that is the expected result, thank you. I will import it into the tool and check it.

    – unknownDev
    Nov 21 '18 at 10:58














0












0








0








If anyone can tell me please how can I do from this flat object structure:



mainObj = {
"Ob1": {
"id": 1,
"name": "Ob1",
"properties": {
"attName": "A1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
},
"Ob2": {
"id": 101,
"name": "Ob2",
"properties": {
"attName": "B1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
"Ob3": {
"id": 10001,
"name": "Ob3",
"properties": {
"attName": "C1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
}


To this one nested in another object, but with the data of the flat one:



myObj = {
"Ob1": {
"myObjName": "A1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob2": {
"myObjName": "B1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob3": {
"myObjName": "C1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
}
}
}
}


The nesting logic is, if the next object's id is bigger than the previous one, then is it's child.
This is the logic:



for each(var obj in mainObj){
switch (true) {
case obj.id < 100: levelId=1; break;
case obj.id < 10000: levelId=2; break;
case obj.id < 1000000: levelId=3; break;
case obj.id < 100000000: levelId=4; break;
}
}


I have just this, but I don't know how to nest them:



for (key in mainObj) {
myObj.myObjName = mainObj[key].properties.attName,
myObj.myObjTyp = mainObj[key].properties.attType,
myObj.myObjOcc = mainObj[key].properties.attOccurance
}


Please if anyone can tell me how can I do this?










share|improve this question
















If anyone can tell me please how can I do from this flat object structure:



mainObj = {
"Ob1": {
"id": 1,
"name": "Ob1",
"properties": {
"attName": "A1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
},
"Ob2": {
"id": 101,
"name": "Ob2",
"properties": {
"attName": "B1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
"Ob3": {
"id": 10001,
"name": "Ob3",
"properties": {
"attName": "C1",
"attType": "string",
"attOccurance": "minOccurs="1""
},
}
}


To this one nested in another object, but with the data of the flat one:



myObj = {
"Ob1": {
"myObjName": "A1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob2": {
"myObjName": "B1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
"Ob3": {
"myObjName": "C1",
"myObjType": "string",
"myObjOcc": "minOccurs="1""
}
}
}
}


The nesting logic is, if the next object's id is bigger than the previous one, then is it's child.
This is the logic:



for each(var obj in mainObj){
switch (true) {
case obj.id < 100: levelId=1; break;
case obj.id < 10000: levelId=2; break;
case obj.id < 1000000: levelId=3; break;
case obj.id < 100000000: levelId=4; break;
}
}


I have just this, but I don't know how to nest them:



for (key in mainObj) {
myObj.myObjName = mainObj[key].properties.attName,
myObj.myObjTyp = mainObj[key].properties.attType,
myObj.myObjOcc = mainObj[key].properties.attOccurance
}


Please if anyone can tell me how can I do this?







javascript object nested structure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:50







unknownDev

















asked Nov 21 '18 at 9:13









unknownDevunknownDev

216




216













  • To make it more clear. Can you pls add 4th object with id 202 and tell what's the output

    – Nitish Narang
    Nov 21 '18 at 9:17













  • btw, how do you access the inner objects? how do you know its name?

    – Nina Scholz
    Nov 21 '18 at 9:17











  • 1. If id 202 appears, it will be on the same level as 101 like in the example. 2. Nina, don't know the inner objects name, but I suppose the for cycle can check if there is an object, right?

    – unknownDev
    Nov 21 '18 at 9:24













  • why not take the children into a children array?

    – Nina Scholz
    Nov 21 '18 at 9:26






  • 1





    @NitishNarang, yes, that is the expected result, thank you. I will import it into the tool and check it.

    – unknownDev
    Nov 21 '18 at 10:58



















  • To make it more clear. Can you pls add 4th object with id 202 and tell what's the output

    – Nitish Narang
    Nov 21 '18 at 9:17













  • btw, how do you access the inner objects? how do you know its name?

    – Nina Scholz
    Nov 21 '18 at 9:17











  • 1. If id 202 appears, it will be on the same level as 101 like in the example. 2. Nina, don't know the inner objects name, but I suppose the for cycle can check if there is an object, right?

    – unknownDev
    Nov 21 '18 at 9:24













  • why not take the children into a children array?

    – Nina Scholz
    Nov 21 '18 at 9:26






  • 1





    @NitishNarang, yes, that is the expected result, thank you. I will import it into the tool and check it.

    – unknownDev
    Nov 21 '18 at 10:58

















To make it more clear. Can you pls add 4th object with id 202 and tell what's the output

– Nitish Narang
Nov 21 '18 at 9:17







To make it more clear. Can you pls add 4th object with id 202 and tell what's the output

– Nitish Narang
Nov 21 '18 at 9:17















btw, how do you access the inner objects? how do you know its name?

– Nina Scholz
Nov 21 '18 at 9:17





btw, how do you access the inner objects? how do you know its name?

– Nina Scholz
Nov 21 '18 at 9:17













1. If id 202 appears, it will be on the same level as 101 like in the example. 2. Nina, don't know the inner objects name, but I suppose the for cycle can check if there is an object, right?

– unknownDev
Nov 21 '18 at 9:24







1. If id 202 appears, it will be on the same level as 101 like in the example. 2. Nina, don't know the inner objects name, but I suppose the for cycle can check if there is an object, right?

– unknownDev
Nov 21 '18 at 9:24















why not take the children into a children array?

– Nina Scholz
Nov 21 '18 at 9:26





why not take the children into a children array?

– Nina Scholz
Nov 21 '18 at 9:26




1




1





@NitishNarang, yes, that is the expected result, thank you. I will import it into the tool and check it.

– unknownDev
Nov 21 '18 at 10:58





@NitishNarang, yes, that is the expected result, thank you. I will import it into the tool and check it.

– unknownDev
Nov 21 '18 at 10:58












1 Answer
1






active

oldest

votes


















1














Given the input and output this is what I have come up with. See if it helps.



Though there were many cases I thought that I am not sure what the output should be.






const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)








share|improve this answer
























  • Can you please explain me what is the idea behind getLastLevel(id) function?

    – unknownDev
    Nov 21 '18 at 12:12











  • getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

    – Nitish Narang
    Nov 21 '18 at 12:15











  • BTW curious to know, did this solution work ?

    – Nitish Narang
    Nov 21 '18 at 12:16






  • 1





    I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

    – unknownDev
    Nov 21 '18 at 12:20






  • 1





    Thanks again @Nitish, it works good!

    – unknownDev
    Nov 23 '18 at 10:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408641%2fcreate-nested-object-structure-from-flat-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Given the input and output this is what I have come up with. See if it helps.



Though there were many cases I thought that I am not sure what the output should be.






const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)








share|improve this answer
























  • Can you please explain me what is the idea behind getLastLevel(id) function?

    – unknownDev
    Nov 21 '18 at 12:12











  • getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

    – Nitish Narang
    Nov 21 '18 at 12:15











  • BTW curious to know, did this solution work ?

    – Nitish Narang
    Nov 21 '18 at 12:16






  • 1





    I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

    – unknownDev
    Nov 21 '18 at 12:20






  • 1





    Thanks again @Nitish, it works good!

    – unknownDev
    Nov 23 '18 at 10:46
















1














Given the input and output this is what I have come up with. See if it helps.



Though there were many cases I thought that I am not sure what the output should be.






const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)








share|improve this answer
























  • Can you please explain me what is the idea behind getLastLevel(id) function?

    – unknownDev
    Nov 21 '18 at 12:12











  • getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

    – Nitish Narang
    Nov 21 '18 at 12:15











  • BTW curious to know, did this solution work ?

    – Nitish Narang
    Nov 21 '18 at 12:16






  • 1





    I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

    – unknownDev
    Nov 21 '18 at 12:20






  • 1





    Thanks again @Nitish, it works good!

    – unknownDev
    Nov 23 '18 at 10:46














1












1








1







Given the input and output this is what I have come up with. See if it helps.



Though there were many cases I thought that I am not sure what the output should be.






const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)








share|improve this answer













Given the input and output this is what I have come up with. See if it helps.



Though there were many cases I thought that I am not sure what the output should be.






const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)








const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)





const mainObj = {"Ob1": {    "id": 1,    "name": "Ob1",    "properties": {        "attName": "A1",        "attType": "string",        "attOccurance": "minOccurs='1'"    },},"Ob2": {     "id": 101,     "name": "Ob2",     "properties": {         "attName": "B1",         "attType": "string",         "attOccurance": "minOccurs='1'"     }, }, "Ob3": {      "id": 10001,      "name": "Ob3",      "properties": {          "attName": "C1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }, }, "Ob4": {      "id": 202,      "name": "Ob4",      "properties": {          "attName": "D1",          "attType": "string",          "attOccurance": "minOccurs='1'"          }   }}

let levelKey = {}, newObj = {}

function getLevel(id) {
let level = 1
while(parseInt(id / 100) > 0) {
level++
id = id / 100
}
return level
}

function getLastLevel(id) {
id--
while(id > 0) {
if(levelKey[id]) return id
id--
}
return id
}

function getObj(str) {
return str.split('.').reduce((o, d) => o[d], newObj)
}

for( let [k, v] of Object.entries(mainObj)) {
let level = getLevel(v['id'])
let obj = {
myObjName: v.properties.attName,
myObjType: v.properties.attType,
myObjOcc: v.properties.attOccurance
}

let lastLevel = getLastLevel(level) || level
levelKey[lastLevel]
? (getObj(levelKey[lastLevel])[k] = obj, levelKey[level] = levelKey[lastLevel] + '.' + k)
: (newObj[k] = obj, levelKey[level] = k)
}

console.log(newObj)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 10:36









Nitish NarangNitish Narang

2,9601815




2,9601815













  • Can you please explain me what is the idea behind getLastLevel(id) function?

    – unknownDev
    Nov 21 '18 at 12:12











  • getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

    – Nitish Narang
    Nov 21 '18 at 12:15











  • BTW curious to know, did this solution work ?

    – Nitish Narang
    Nov 21 '18 at 12:16






  • 1





    I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

    – unknownDev
    Nov 21 '18 at 12:20






  • 1





    Thanks again @Nitish, it works good!

    – unknownDev
    Nov 23 '18 at 10:46



















  • Can you please explain me what is the idea behind getLastLevel(id) function?

    – unknownDev
    Nov 21 '18 at 12:12











  • getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

    – Nitish Narang
    Nov 21 '18 at 12:15











  • BTW curious to know, did this solution work ?

    – Nitish Narang
    Nov 21 '18 at 12:16






  • 1





    I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

    – unknownDev
    Nov 21 '18 at 12:20






  • 1





    Thanks again @Nitish, it works good!

    – unknownDev
    Nov 23 '18 at 10:46

















Can you please explain me what is the idea behind getLastLevel(id) function?

– unknownDev
Nov 21 '18 at 12:12





Can you please explain me what is the idea behind getLastLevel(id) function?

– unknownDev
Nov 21 '18 at 12:12













getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

– Nitish Narang
Nov 21 '18 at 12:15





getLastLevel method is to get which level we need to add the current object to. So, if current obj is at level 2 and you pass to this method it returns 1 means this object should be inside 1st object.

– Nitish Narang
Nov 21 '18 at 12:15













BTW curious to know, did this solution work ?

– Nitish Narang
Nov 21 '18 at 12:16





BTW curious to know, did this solution work ?

– Nitish Narang
Nov 21 '18 at 12:16




1




1





I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

– unknownDev
Nov 21 '18 at 12:20





I will inform you, I need to simplify the solution a little bit, because some of the methods/functions are not recognized. Thanks

– unknownDev
Nov 21 '18 at 12:20




1




1





Thanks again @Nitish, it works good!

– unknownDev
Nov 23 '18 at 10:46





Thanks again @Nitish, it works good!

– unknownDev
Nov 23 '18 at 10:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408641%2fcreate-nested-object-structure-from-flat-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?