I'm currently grappling with the task of converting this object hash:
"food": {
"healthy": {
"fruits": ['apples', 'bananas', 'oranges'],
"vegetables": ['salad', 'onions']
},
"unhealthy": {
"fastFood": ['burgers', 'chicken', 'pizza']
}
}
into a format like this:
food:healthy:fruits:apples
food:healthy:fruits:bananas
food:healthy:fruits:oranges
food:healthy:vegetables:salad
food:healthy:vegetables:onions
food:unhealthy:fastFood:burgers
food:unhealthy:fastFood:chicken
food:unhealthy:fastFood:pizza
In essence, it involves looping through the object while keeping track of the path and generating the desired output.
However, I'm unsure how to recursively loop until all nested elements are processed.
var path;
var pointer;
function loop(obj) {
for (var propertyName in obj) {
path = propertyName;
pointer = obj[propertyName];
if (pointer typeof === 'object') {
loop(pointer);
} else {
break;
}
}
};
function parse(object) {
var collection = [];
};
There are two conflicting issues at play here:
- If I use recursive programming, it loses track of properties that have already been parsed.
- If I don't use recursion, I can't handle infinite nesting.
Any ideas on how to tackle this challenge would be greatly appreciated.
Best regards