Hello guys I'm trying to parse an array of strings into a custom structure:
var str = [
"country.UK.level.1",
"country.UK.level.2",
"country.US.level.1",
"country.UK.level.3"
];
Into something like:
var ordered = {
"country": [
{"UK" : {"level" : ["1", "2", "3"]}},
{"US" : {"level" : ["1","2"]}}
]
}
Notes:
- Strings stored in the
strarray will not be sorted and the code should be robust against that. - Strings will follow the
x.y.x.y...pattern, wherexwill be unique for that array andycan change. In my examplecountryandlevelwill always be the same as they represent thexpos. - This requires recursive approach as the strings stored in the
strarray, can be of any length. The longer the string the deeper nesting.