I need to create an object from a JSON file. The Input Json file looks like this.
{
"test":{
"Record":1,
"RecordValues":{
"Address":"3330 Bay Rd.",
"City":"Los Angeles",
"SecondObject":{
"1":"eins",
"2":"zwei"
}
}
}
}
What i have so far is this function..
var test = [];
function recFunc(obj, parent_id = null) {
for(var i in obj) {
if(typeof obj[i] == "object" && obj[i] !== null) {
test.push({title: i, children: []});
recFunc(obj[i], (test.length-1));
}else {
if(parent_id != null) {
test[parent_id].children.push({title: (i + " : " + obj[i])});
}else {
test.push({title: (i + " : " + obj[i])});
}
}
}
return test;
}
The output object should be as follows.
[
{ "title":"Record : 1" },
{
"title":"RecordValues",
"children":[
{ "title":"Address : 3330 Bay Rd." },
{ "title":"City : Los Angeles" },
{
"title":"SecondObject",
"children":[
{ "title":"1 : eins" },
{ "title":"2 : zwei" }
]
}
]
}
]
georg
216k57 gold badges325 silver badges401 bronze badges
-
1That target object looks like a very bad design. Can you explain why you need that?trincot– trincot2019年04月13日 07:57:14 +00:00Commented Apr 13, 2019 at 7:57
-
@trincot the target object is needed for javascript plugin fancytreeVinnecent– Vinnecent2019年04月13日 08:08:39 +00:00Commented Apr 13, 2019 at 8:08
3 Answers 3
You can use Object.entries and reduce with recursion
let obj = {"test":{"Record":1,"RecordValues":{"Address":"3330 Bay Rd.","City":"Los Angeles","SecondObject":{"1":"eins","2":"zwei"}}}}
let recursive = (obj) =>
Object.entries(obj).reduce((op,[key,value]) => {
let final = typeof value === 'object' ? {title:key, children:recursive(value)}
: {title: `${key}: ${value}`}
return op.concat(final)
},[])
console.log(recursive(obj.test))
answered Apr 13, 2019 at 8:08
Code Maniac
37.9k5 gold badges44 silver badges65 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Here's one possible approach, recursively iterating over the Object.entries:
const input = {
"test": {
"Record": 1,
"RecordValues": {
"Address": "3330 Bay Rd.",
"City": "Los Angeles",
"SecondObject": {
"1": "eins",
"2": "zwei"
}
}
}
};
const makeNested = (currInput) => {
return Object.entries(currInput).map(([key, val]) => (
typeof val !== 'object'
? { title: `${key} : ${val}` }
: {
title: key,
children: makeNested(val)
}
))
};
console.log(makeNested(input.test));
answered Apr 13, 2019 at 8:04
CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
1 Comment
Vinnecent
I will try that when i come home. Thank you very much :)
You could use Object.entries() and recursively call the function if the nested value is an object:
(The Object(value) === value checks if the value is an object which is not null)
const obj = {test:{Record:1,RecordValues:{Address:"3330 Bay Rd.",City:"Los Angeles",SecondObject:{1:"eins",2:"zwei"}}}};
function transform(obj) {
return Object.entries(obj).map(([key, value]) => {
if(Object(value) === value)
return { title: key, children: transform(value) }
else
return { title: `${key} ${value}` }
})
}
console.log(transform(obj.test))
answered Apr 13, 2019 at 8:04
adiga
35.4k9 gold badges66 silver badges88 bronze badges
2 Comments
Code Maniac
You reached here faster +1
Code Maniac
and i was messing around using reduce :p i need to practice more about recursion for sure :)
Explore related questions
See similar questions with these tags.
lang-js