Access attribute of a JS object using as key the key of another attribute

0

I'm trying to get the value inside a given object using the key of another object: Ex:

I have the objects:

var obj1 = { id:'gh73f'}

var obj2 = {
    gh73f : 123,
    h39sg : 764,
    c9wer : 921
}

I've tried:

var valor = obj2[obj1.id];

and

var valor = obj2[obj1[id]];

In none of the cases can I get the value of the key gh73f of the obj2 .

    
asked by anonymous 27.05.2014 / 14:22

1 answer

2

Do this by using eval , mount the string and then eval to run as code:

var valor = eval("obj2."+obj1.id);
    
27.05.2014 / 14:29