I need to process files that are structured like
Title
/foo/bar/foo/bar 1
/foo/bar/bar 2
/bar/foo 3
/bar/foo/bar 4
It's easy enough to parse this into an array of arrays, by splitting at every / and \n. However, once I get an array of arrays, I can't figure out a good way to turn that into nested objects. Desired format:
{
Title,
{
foo: {
bar: {
foo: {
bar: 1
},
bar: 2
}
},
bar: {
foo: {
3,
bar: 4
}
}
}
}
This seems like a super common thing to do, so I'm totally stumped as to why I can't find any pre-existing solutions. I sort of expected javascript to even have a native function for this, but merging objects apparently overrides values instead of making a nested object, by default. I've tried the following, making use of jQuery.extend(), but it doesn't actually combine like-terms (i.e. parents and grand-parents).
let array = fileContents.split('\n');
let object = array.map(function(string) {
const result = string.split('/').reduceRight((all, item) => ({
[item]: all
}), {});
return result;
});
output = $.extend(true, object);
console.log(JSON.stringify(output));
This turned the array of arrays into nested objects, but didn't merge like-terms... I could brute-force this, but my real-world problem has over 2000 lines, goes 5 layers deep (/foo/foo/foo/foo value value value), and actually has an array of space-separated values rather than a single value per line. I'm willing to treat the array of values like a string and just pretend its not an array, but it would be really nice to at least get the objects nested properly without writing a hefty/primitive algorithm.
Since this is essentially how subdirectories are organized, it seems like this should be an easy one. Is there a relatively simple solution I'm not seeing?