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?
javascript object nested
add a comment |
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?
javascript object nested
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
add a comment |
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?
javascript object nested
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
javascript object nested
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
add a comment |
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)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)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
add a comment |
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
add a comment |
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%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
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
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