Fill empty object with part of the data from another object JavaScript











up vote
-1
down vote

favorite












I have an empty object that's represented like this:



myObj = {
myObjName = null;
myObjType = null;
myObjOcc = null;
}


And there is an object with objects represented like this:



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""
},
}
}


I have to insert the data from the objects in mainObj to myObj.



The mapping is supposed to be like this:



myObjName = attName
myObjType = attType
myObjOcc = attOccurance


Another thing taken into account has to be id in mainObj, because Ob2 has to be nested inside Ob1 and Ob3 has to be nested inside Ob2



So myObj is suppose to look like this:



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""
}
}
}
}


I have this part in the code that determines which level is every object:



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


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










share|improve this question




















  • 1




    So myObj is suppose to look like this: ..... The sample doesn't look nested to me. You just made some indentation illusion
    – Abana Clara
    yesterday












  • Sorry, you're right, it's fixed
    – unknownDev
    yesterday















up vote
-1
down vote

favorite












I have an empty object that's represented like this:



myObj = {
myObjName = null;
myObjType = null;
myObjOcc = null;
}


And there is an object with objects represented like this:



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""
},
}
}


I have to insert the data from the objects in mainObj to myObj.



The mapping is supposed to be like this:



myObjName = attName
myObjType = attType
myObjOcc = attOccurance


Another thing taken into account has to be id in mainObj, because Ob2 has to be nested inside Ob1 and Ob3 has to be nested inside Ob2



So myObj is suppose to look like this:



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""
}
}
}
}


I have this part in the code that determines which level is every object:



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


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










share|improve this question




















  • 1




    So myObj is suppose to look like this: ..... The sample doesn't look nested to me. You just made some indentation illusion
    – Abana Clara
    yesterday












  • Sorry, you're right, it's fixed
    – unknownDev
    yesterday













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have an empty object that's represented like this:



myObj = {
myObjName = null;
myObjType = null;
myObjOcc = null;
}


And there is an object with objects represented like this:



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""
},
}
}


I have to insert the data from the objects in mainObj to myObj.



The mapping is supposed to be like this:



myObjName = attName
myObjType = attType
myObjOcc = attOccurance


Another thing taken into account has to be id in mainObj, because Ob2 has to be nested inside Ob1 and Ob3 has to be nested inside Ob2



So myObj is suppose to look like this:



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""
}
}
}
}


I have this part in the code that determines which level is every object:



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


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










share|improve this question















I have an empty object that's represented like this:



myObj = {
myObjName = null;
myObjType = null;
myObjOcc = null;
}


And there is an object with objects represented like this:



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""
},
}
}


I have to insert the data from the objects in mainObj to myObj.



The mapping is supposed to be like this:



myObjName = attName
myObjType = attType
myObjOcc = attOccurance


Another thing taken into account has to be id in mainObj, because Ob2 has to be nested inside Ob1 and Ob3 has to be nested inside Ob2



So myObj is suppose to look like this:



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""
}
}
}
}


I have this part in the code that determines which level is every object:



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


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







javascript object nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









unknownDev

196




196








  • 1




    So myObj is suppose to look like this: ..... The sample doesn't look nested to me. You just made some indentation illusion
    – Abana Clara
    yesterday












  • Sorry, you're right, it's fixed
    – unknownDev
    yesterday














  • 1




    So myObj is suppose to look like this: ..... The sample doesn't look nested to me. You just made some indentation illusion
    – Abana Clara
    yesterday












  • Sorry, you're right, it's fixed
    – unknownDev
    yesterday








1




1




So myObj is suppose to look like this: ..... The sample doesn't look nested to me. You just made some indentation illusion
– Abana Clara
yesterday






So myObj is suppose to look like this: ..... The sample doesn't look nested to me. You just made some indentation illusion
– Abana Clara
yesterday














Sorry, you're right, it's fixed
– unknownDev
yesterday




Sorry, you're right, it's fixed
– unknownDev
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













What you're saying are objects look an awful like JSON. However, here's how you'd accomplish what you say you want the result to be.






// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)








share|improve this answer





















  • Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
    – unknownDev
    yesterday













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',
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%2f53370634%2ffill-empty-object-with-part-of-the-data-from-another-object-javascript%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








up vote
0
down vote













What you're saying are objects look an awful like JSON. However, here's how you'd accomplish what you say you want the result to be.






// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)








share|improve this answer





















  • Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
    – unknownDev
    yesterday

















up vote
0
down vote













What you're saying are objects look an awful like JSON. However, here's how you'd accomplish what you say you want the result to be.






// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)








share|improve this answer





















  • Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
    – unknownDev
    yesterday















up vote
0
down vote










up vote
0
down vote









What you're saying are objects look an awful like JSON. However, here's how you'd accomplish what you say you want the result to be.






// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)








share|improve this answer












What you're saying are objects look an awful like JSON. However, here's how you'd accomplish what you say you want the result to be.






// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)








// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)





// var myObj = {};
// for (key in mainObj) {
// const name = mainObj[key].name
// myObj[name] = {
// myObjName: mainObj[key].properties.attName,
// myObjType: mainObj[key].properties.attType,
// myObjOcc: mainObj[key].properties.attOccurance
// }
// }

// console.log(myObj)

var 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"'
},
}
}

var myObj = {};
for (key in mainObj) {
const name = mainObj[key].name
myObj[name] = {
myObjName: mainObj[key].properties.attName,
myObjType: mainObj[key].properties.attType,
myObjOcc: mainObj[key].properties.attOccurance
}
}

console.log(myObj)






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









AnonymousSB

51712




51712












  • Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
    – unknownDev
    yesterday




















  • Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
    – unknownDev
    yesterday


















Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
– unknownDev
yesterday






Thanks for the answer, but in this solution, the objects are not nested inside each other. How can I do that using the levelId inside case function?
– unknownDev
yesterday




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370634%2ffill-empty-object-with-part-of-the-data-from-another-object-javascript%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

Feedback on college project

Futebolista

Albești (Vaslui)