I have a function...
function createDatabase(data){
items = {};
for (field in data){
if (typeof data[field] == "object"){
items.field = createDatabase(data[field]);
} else {
topic = data[field]
items[field] = topic;
}
};
return items;
}
And I call it with...
result = {"fields": createDatabase(jsonData)};
console.log(result);
My problem is that the result is only ever the final result of the recursive function calls. So the output is, for example...
{
fields: {
'0': 'Red',
'1': 'White',
'2': 'Blue'
}
}
But red, white and blue don't appear until the end of a 3,000+ line file. What I'm aiming for is to get a sort of replicated version of the original JSON file, with embedded documents and all. I'll be doing something else obviously, but I'm trying to nail this iterative process first. It's like every time the code runs, it overwrites what was there before.
I'd like this line below to add on the results from another function call to an object:
items.field = createDatabase(data[field]);
EDIT
The start of my JSON file looks like this:
{
"Applied Sciences": {
"Agriculture": {
"Agricultural Economics": [
"Agricultural Environment And Natural Resources",
"Developmental Economics",
"Food And Consumer Economics",
"Production Economics And Farm Management"
],
"Agronomy": [
"Agroecology",
"Biotechnology",
"Plant Breeding",
"Soil Conservation",
"Soil Science",
"Theoretical Modeling"
],
I am hoping to make the output of my code basically resemble this. Once I get this working, at each stage, I want to add some stuff to each item to build a database schema with, but I can't get past this first part. All I end up with is the final array of the final object at the bottom of the file. I have changed ".field" to "[field]" in the function, but this still doesn't help.