Assume
{
"foo":[
"baz"
],
"bar": {
"blarg": [
"blippo"
],
"blunder": {
"bus": [
{
"bigly": [
"bugnuts"
]
}
]
}
},
"blather": [
{
"bumpy": [
"bugaloo"
]
},
{
"blither": {
"bother": [
"bingo"
]
}
}
]
}
What would be the most efficient way (preferably using lodash) to convert such that all leaves that are arrays of one member now contain that member, and not the array? As in:
{
"foo": "baz",
"bar": {
"blarg": "blippo",
"blunder": {
"bus": {
"bigly": "bugnuts"
}
}
},
"blather": [
{
"bumpy": "bugaloo"
},
{
"blither": {
"bother": "bingo"
}
}
]
}
The object is much larger than the one I've presented here, and so has many possible paths.
I've tried first getting a list of paths, as in:
foo[0]
foo
bar.blarg[0]
bar.blarg
bar.blunder.bus[0]
bar.blunder.bus
bar.blunder
bar
blather[0].bumpy[0]
blather[0].bumpy
blather[0]
blather[1].blither.bother[0]
blather[1].blither.bother
blather[1].blither
blather[1]
blather
and attempting to do the mutation both depth and breadth first, but of course, the first mutation has the possibility of invalidating other paths.
I'm thinking it's a question of recursion, but the solution eludes me.