0

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?

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
Ændri Domi
  • 111
  • 9

7 Answers7

3

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'))
R. Foubert
  • 633
  • 4
  • 8
  • Thanks but this is the same as hard coded... i have to use the same function in object that have different keys and expecially different sub level,for example obj.theme.menu.color – Ændri Domi Oct 19 '16 at 14:06
  • It's exactly the same idea... You just need to have split the string and for each value go through the object. I update my answer to be clearer – R. Foubert Oct 19 '16 at 14:08
  • I updated my answer. You can still do something better by throwing some errors if you don't find the substring... It can be improved but I think it's the way to go ;) – R. Foubert Oct 19 '16 at 14:17
0

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]]);
Nicolas Arias
  • 60
  • 1
  • 4
0

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);
}
JoshBerke
  • 66,142
  • 25
  • 126
  • 164
0

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));
Kevin Kloet
  • 1,086
  • 1
  • 11
  • 21
0

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

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
Owuor
  • 616
  • 6
  • 10
0

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.

Community
  • 1
  • 1
pius lee
  • 1,164
  • 1
  • 12
  • 28