I've an object like this :
var routes = {
'fr-FR': [
'/accueil',
'/offre',
'/specifications'
],
'en-US': [
'/home',
'/offer',
'/specifications-en'
],
'de-DE': [
'/startseite',
'/annbod',
'/specificaties'
]
};
And my goal is to get the locale according to an url.
I tried something like : (url = /offer)
var getLocale = function(url) {
console.log('getLocale for %s', url);
for(var language in routes) {
for(var route in language) {
console.log('lg = ' + language);
console.log('rt = ' + route);
if (route === url) {
return route;
}
}
}
};
And the output I get is :
getLocale for /offer
lg = fr-FR
rt = 0
lg = fr-FR
rt = 1
lg = fr-FR
rt = 2
lg = fr-FR
rt = 3
lg = fr-FR
rt = 4
lg = en-US
rt = 0
lg = en-US
rt = 1
lg = en-US
rt = 2
lg = en-US
rt = 3
lg = en-US
rt = 4
lg = de-DE
rt = 0
lg = de-DE
rt = 1
lg = de-DE
rt = 2
lg = de-DE
rt = 3
lg = de-DE
rt = 4
So I guess I'm doing well for the first for-in loop but I don't know how to loop in the array instead of the chars of its name ...
Also, I read on many SO posts that for-in loops are not the best way to loop in certain cases but which ones?
Thanks in advance.
for..inloops over an object's keys, not its values.