Reputation: 111
I have an object:
var obj= {
hello:{
it:"ciao",
en:"hello"
}
}
Now the problem is that i can't access a value through obj.hello[lang]
, neither obj.hello.en
.
i have a string like 'hello.it' or 'hello.en' and I want to get something like obj[myString] that became obj.hello.en
I tried to split the string with .split('.')
, but i have to loop hard coded through the length result
How can i achieve this?
Upvotes: 0
Views: 137
Reputation: 1086
this should work:
var obj= {
hello:{
it: "ciao",
en: "hello"
}
};
var input = "hello.it"; //change this to the value you wish to get.
var str = "obj".concat(".").concat(input);
console.log(eval(str));
Upvotes: 0
Reputation: 67088
Here's a sample showing how you can walk down an object:
var obj= {
hello:{
it:"ciao",
en:"hello"
}
}
var path='hello.it'.split('.');
x=obj;
for (var i=0;i<path.length;i++)
{
x=x[path[i]];
console.log(x);
}
Edit To modify the value you can use the same style, but you need to know when your at the string. This really will depend on your requirements and your actual object model but here's an example
for (var i=0;i<path.length;i++)
{
a=x[path[i]];
if(typeof(a)=='string')
{
x[path[i]]='NewString'
console.log(x[path[i]]);
break;
}
x=a
console.log(x);
}
Upvotes: 0
Reputation: 1174
Personaly, I don't know why you don't use eval
.
var str = "hello.it";
eval("obj." + str);
I thin eval is best if str is hardcoded.
And I think you can refer this.
Upvotes: 0
Reputation: 616
Try this, it will work for whichever level you want.
var obj= {
hello:{
it:"ciao",
en:"hello"
}
}
var lang='hello.it';
var val=obj;
lang.split('.').forEach(function(c){val=val[c]});
console.log(val);//Should output ciao
Upvotes: 0
Reputation: 386654
You could split the path and iterate it and reduce the object you have. This proposal works with a default value for missing properties.
var obj= { hello: { it: "ciao", en: "hello" } },
path ='hello.it',
value = path.split('.').reduce(function (o, k) {
return (o || {})[k];
}, obj);
console.log(value);
Upvotes: 0
Reputation: 653
I don't understand... obj.hello.it should work
var obj= {
hello:{
it:"ciao",
en:"hello"
}
}
console.log(obj.hello.it);
If you need to get this value from a string 'hello.it' I would do something like that :
var obj= {
hello:{
it:"ciao",
en:"hello"
}
}
var helloIt = 'hello.it';
var helloEn = 'hello.en';
function translate(s){
var values = s.split('.');
var resource = values[0];
var lang = values[1];
return obj[resource][lang];
}
console.log(translate(helloIt));
console.log(translate(helloEn));
After that you have to manage some cases (if the string has not the right format, if the translation does not exist...). You could manage that everything in a translation module or something... Hope it helps ;)
EDIT :
If you want to have a way to 'explore' objects using a string you can do something like that :
var obj= {
hello:{
it:"ciao",
en:"hello"
}
};
function explore(o, s){
return s.split('.').reduce(function(r, ss){
return r && r[ss] ? r[ss] : null;
}, o);
}
console.log(explore(obj, 'hello.it'))
Upvotes: 3
Reputation: 60
Hi if your string is something like 'hello.it' you can do this hope it helps
var obj= {
hello:{
it:"ciao",
en:"hello"
}
};
var str = 'hello.it';
alert(obj[t.split('.')[0]][t.split('.')[1]]);
Upvotes: 0