How to convert one object to another one using recursion. Javascript
up vote
1
down vote
favorite
i don't know the way so i need help please. So i have this object
{
"label": "root",
"children": [
{
"label": "a",
"value": "abc"
},
{
"label": "b",
"value": 123
},
{
"label": "c",
"children": [
{
"label": 0,
"children": [
{
"label": "d",
"value": "def"
},
{
"label": "e",
"value": 1
},
{
"label": "f",
"children": [
{
"label": 0,
"children": [
{
"label": "g",
"value": "ghi"
},
{
"label": "h",
"value": 456
}
]
}
]
}
]
},
{
"label": 1,
"children": [
{
"label": "i",
"value": 1
},
{
"label": "j",
"value": "abc"
},
{
"label": "asd",
"children": [
{
"label": 0,
"children": [
{
"label": "qwe",
"value": "kuracpalac"
},
{
"label": "rtz",
"value": "asdasdasdasd"
}
]
}
]
}
]
}
]
}
]
}
, and i need it to convert it to new object that looks like this one :
const obj = {
a: "abc",
b: 123,
c: [
{
d: "def",
e: 1,
f: [
{
g: "ghi",
h: 456
}
]
},
{
i: 1,
j: "abc",
asd: {
qwe: "kuracpalac",
rtz: "asdasdasdasd"
}
}
]
};
Thank you for you help :)!
javascript arrays frontend javascript-objects
add a comment |
up vote
1
down vote
favorite
i don't know the way so i need help please. So i have this object
{
"label": "root",
"children": [
{
"label": "a",
"value": "abc"
},
{
"label": "b",
"value": 123
},
{
"label": "c",
"children": [
{
"label": 0,
"children": [
{
"label": "d",
"value": "def"
},
{
"label": "e",
"value": 1
},
{
"label": "f",
"children": [
{
"label": 0,
"children": [
{
"label": "g",
"value": "ghi"
},
{
"label": "h",
"value": 456
}
]
}
]
}
]
},
{
"label": 1,
"children": [
{
"label": "i",
"value": 1
},
{
"label": "j",
"value": "abc"
},
{
"label": "asd",
"children": [
{
"label": 0,
"children": [
{
"label": "qwe",
"value": "kuracpalac"
},
{
"label": "rtz",
"value": "asdasdasdasd"
}
]
}
]
}
]
}
]
}
]
}
, and i need it to convert it to new object that looks like this one :
const obj = {
a: "abc",
b: 123,
c: [
{
d: "def",
e: 1,
f: [
{
g: "ghi",
h: 456
}
]
},
{
i: 1,
j: "abc",
asd: {
qwe: "kuracpalac",
rtz: "asdasdasdasd"
}
}
]
};
Thank you for you help :)!
javascript arrays frontend javascript-objects
1
You should always try to include anything you have tried in your questions so people can help nudge you in the correct direction or see where your failure of understanding is.
– Marie
Nov 21 at 15:31
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i don't know the way so i need help please. So i have this object
{
"label": "root",
"children": [
{
"label": "a",
"value": "abc"
},
{
"label": "b",
"value": 123
},
{
"label": "c",
"children": [
{
"label": 0,
"children": [
{
"label": "d",
"value": "def"
},
{
"label": "e",
"value": 1
},
{
"label": "f",
"children": [
{
"label": 0,
"children": [
{
"label": "g",
"value": "ghi"
},
{
"label": "h",
"value": 456
}
]
}
]
}
]
},
{
"label": 1,
"children": [
{
"label": "i",
"value": 1
},
{
"label": "j",
"value": "abc"
},
{
"label": "asd",
"children": [
{
"label": 0,
"children": [
{
"label": "qwe",
"value": "kuracpalac"
},
{
"label": "rtz",
"value": "asdasdasdasd"
}
]
}
]
}
]
}
]
}
]
}
, and i need it to convert it to new object that looks like this one :
const obj = {
a: "abc",
b: 123,
c: [
{
d: "def",
e: 1,
f: [
{
g: "ghi",
h: 456
}
]
},
{
i: 1,
j: "abc",
asd: {
qwe: "kuracpalac",
rtz: "asdasdasdasd"
}
}
]
};
Thank you for you help :)!
javascript arrays frontend javascript-objects
i don't know the way so i need help please. So i have this object
{
"label": "root",
"children": [
{
"label": "a",
"value": "abc"
},
{
"label": "b",
"value": 123
},
{
"label": "c",
"children": [
{
"label": 0,
"children": [
{
"label": "d",
"value": "def"
},
{
"label": "e",
"value": 1
},
{
"label": "f",
"children": [
{
"label": 0,
"children": [
{
"label": "g",
"value": "ghi"
},
{
"label": "h",
"value": 456
}
]
}
]
}
]
},
{
"label": 1,
"children": [
{
"label": "i",
"value": 1
},
{
"label": "j",
"value": "abc"
},
{
"label": "asd",
"children": [
{
"label": 0,
"children": [
{
"label": "qwe",
"value": "kuracpalac"
},
{
"label": "rtz",
"value": "asdasdasdasd"
}
]
}
]
}
]
}
]
}
]
}
, and i need it to convert it to new object that looks like this one :
const obj = {
a: "abc",
b: 123,
c: [
{
d: "def",
e: 1,
f: [
{
g: "ghi",
h: 456
}
]
},
{
i: 1,
j: "abc",
asd: {
qwe: "kuracpalac",
rtz: "asdasdasdasd"
}
}
]
};
Thank you for you help :)!
javascript arrays frontend javascript-objects
javascript arrays frontend javascript-objects
edited Nov 21 at 15:08
Randy Casburn
4,0751218
4,0751218
asked Nov 21 at 15:07
Lex
82
82
1
You should always try to include anything you have tried in your questions so people can help nudge you in the correct direction or see where your failure of understanding is.
– Marie
Nov 21 at 15:31
add a comment |
1
You should always try to include anything you have tried in your questions so people can help nudge you in the correct direction or see where your failure of understanding is.
– Marie
Nov 21 at 15:31
1
1
You should always try to include anything you have tried in your questions so people can help nudge you in the correct direction or see where your failure of understanding is.
– Marie
Nov 21 at 15:31
You should always try to include anything you have tried in your questions so people can help nudge you in the correct direction or see where your failure of understanding is.
– Marie
Nov 21 at 15:31
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You could use two functions, one for getting either an array, depending on the keys, if integer values or an assigned object. The other function returns an object with a new key/value pair, either a value or a nested object structure of the children array.
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You could use two functions, one for getting either an array, depending on the keys, if integer values or an assigned object. The other function returns an object with a new key/value pair, either a value or a nested object structure of the children array.
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
add a comment |
up vote
2
down vote
accepted
You could use two functions, one for getting either an array, depending on the keys, if integer values or an assigned object. The other function returns an object with a new key/value pair, either a value or a nested object structure of the children array.
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You could use two functions, one for getting either an array, depending on the keys, if integer values or an assigned object. The other function returns an object with a new key/value pair, either a value or a nested object structure of the children array.
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could use two functions, one for getting either an array, depending on the keys, if integer values or an assigned object. The other function returns an object with a new key/value pair, either a value or a nested object structure of the children array.
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
function assign(array) {
return Object.assign(0 in array[0] ? : {}, ...array);
}
function getObject({ label, value, children }) {
return { [label]: value || assign(children.map(getObject)) };
}
var data = { label: "root", children: [{ label: "a", value: "abc" }, { label: "b", value: 123 }, { label: "c", children: [{ label: 0, children: [{ label: "d", value: "def" }, { label: "e", value: 1 }, { label: "f", children: [{ label: 0, children: [{ label: "g", value: "ghi" }, { label: "h", value: 456 }] }] }] }, { label: 1, children: [{ label: "i", value: 1 }, { label: "j", value: "abc" }, { label: "asd", children: [{ label: 0, children: [{ label: "qwe", value: "kuracpalac" }, { label: "rtz", value: "asdasdasdasd" }] }] }] }] }] },
result = getObject(data).root;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
edited Nov 21 at 15:35
answered Nov 21 at 15:30
Nina Scholz
172k1384147
172k1384147
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
add a comment |
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
Thank you very much! This helped!
– Lex
Nov 21 at 15:38
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53414956%2fhow-to-convert-one-object-to-another-one-using-recursion-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
You should always try to include anything you have tried in your questions so people can help nudge you in the correct direction or see where your failure of understanding is.
– Marie
Nov 21 at 15:31