Create nested object structure from flat one
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
|
show 15 more comments
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
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
|
show 15 more comments
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
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
javascript object nested structure
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
|
show 15 more comments
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
|
show 15 more comments
1 Answer
1
active
oldest
votes
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)
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 returns1
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
|
show 7 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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 returns1
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
|
show 7 more comments
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)
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 returns1
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
|
show 7 more comments
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)
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)
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 returns1
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
|
show 7 more comments
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 returns1
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
|
show 7 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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