This question is not an duplicate of Fastest way to flatten / un-flatten nested JSON objects, because additional values become keys, the order changes largely and my object is not simply "in a line".
I have updated the question description to make it clearer.
I have a nested object which represents a multilanguage path to controller mapping:
{
"welcome": {
"news": {
"de": "nachrichten",
"en": "news"
},
"de": "willkommen",
"en": "welcome"
},
"applications": {
"application1": {
"de": "anwendung1",
"en": "application1"
},
"application2": {
"features": {
"de": "funktionen",
"en": "features"
},
"de": "anwendung2",
"en": "application2"
},
"de": "anwendungen",
"en": "applications"
}
}
This should be converted to an easy-to-use object that accepts a path as key:
{
"/de/willkommen/": "welcome",
"/en/welcome/": "welcome",
"/de/willkommen/nachrichten/": "news",
"/en/welcome/news/": "news",
"/de/anwendungen/": "applications",
"/en/applications/": "applications",
"/de/anwendungen/anwendung1/": "application1",
"/en/applications/application1/": "application1",
"/de/anwendungen/anwendung2/": "application2",
"/en/applications/application2/": "application2",
"/de/anwendungen/anwendung2/funktionen/": "features",
"/en/applications/application2/features/": "features",
}
Now the initial language specific values ("de": "willkommen" etc) buildung the path and are the key and the initial key is the value. But please take a look, it's a little bit more complex.
I have build a function, but they work only for the first level like "/de/anwendungen/", not for "/de/anwendungen/anwendung1/" and lower.
convertToPath(OldObject, NewObject = {})
{
for(let SecondObject in OldObject)
{
for(let Key in OldObject[SecondObject])
{
NewObject["/" + Key + "/" + OldObject[SecondObject][Key] + "/"] = SecondObject;
}
}
return NewObject;
}