I have an Object structured like this:
{
_id: {
$oid: 'foo',
},
data: {
john: {
_doe: {
$oid: 'bar'
}
}
}
}
Now I want to massage this object so that it becomes:
{
_id: 'foo',
data: {
john: {
_doe: 'bar'
}
}
}
In other words, every instance of $oid should move that value up one level (or something like that). It could occur at many levels.
I tried:
var loop = function(o) {
for ( var i in o ) {
if ( i == '$oid' ) {
o = o[i];
} else if ( typeof o[i] == 'object' ) {
loop(o[i]);
}
}
return o;
}
loop(obj);
But it fails, and I can’t get my head around it…
You almost have it, just needed a small fix:
You need to assign the result of loop to the original object.