Given the following javascript object structure, which is typically what is returned by firebase
var data = {
'id1' : {
'fname' : 'value',
'lname' : 'value',
'things' : {
'thing1' : {
'brand' : 'value',
'name' : 'value'
},
'thing2' : {
'brand' : 'value',
'name' : 'value'
},
'thing3' : {
'brand' : 'value',
'name' : 'value'
}
}
},
'id2' : {
'fname' : 'value',
'lname' : 'value',
'things' : {
'thing1' : {
'brand' : 'value',
'name' : 'value'
},
'thing2' : {
'brand' : 'value',
'name' : 'value'
},
'thing3' : {
'brand' : 'value',
'name' : 'value'
}
}
}
};
How would you convert it to
[
{
'fname' : 'value',
'lname' : 'value,
'things' : [
{
'brand' : 'value',
'name' : 'value'
},
{
'brand' : 'value',
'name' : 'value'
},
{
'brand' : 'value',
'name' : 'value'
}
]
},
{
'fname' : 'value',
'lname' : 'value,
'things' : [
{
'brand' : 'value',
'name' : 'value'
},
{
'brand' : 'value',
'name' : 'value'
},
{
'brand' : 'value',
'name' : 'value'
}
]
}
]
Keeping in mind that the nested objects could get deeper than this. There are many implementations to convert nested structures to arrays, but I have not seen anyone make this specific conversion. I've been racking my brain for a while and have only come very close but have yet to get it just right.
I already have this code which doesn't do anything with deeper objects like 'things'.
var collection = [];
for(key in data) {
collection.push(data[key]);
}
I'm struggling with making it recursive.
EDIT I would imagine it needs to be in wrapped in a function that can call itself so that it can be made recursive.