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?

2
  • 1
    Trivial to do with lodash: _.get(obj, 'hello.it') Commented Oct 19, 2016 at 13:56
  • 1
    Thanks :) _.set function was exactly what i was looking for Commented Oct 20, 2016 at 10:08

7 Answers 7

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'))

Sign up to request clarification or add additional context in comments.

3 Comments

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
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
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 ;)
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]]);

Comments

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);

Comments

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

Comments

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.

Comments

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);
}

2 Comments

Your seems the most accurate to the answer, it works to get a value but my scope is to edit rather then get the value
@ÆndriDomi: Have a look you can modify the value as well
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));

3 Comments

Yes i know in this way it works , but i have the index as string like 'hello.it' it obviously doesn't work with obj["hello.it"]
i have edited it so it will work with whatever input you desire
Depending on where the string comes from eval() could be dangerous

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.