Sternjobname
Sternjobname

Reputation: 779

Javascript use variable to call another variable

I'm making something in React Native and there's a module where I have a variable called jorge

const jorge = "This is some output"; 
console.log(jorge);            //output: 'This is some output'

I've passed props item to this module (I'm using the react-native-router-flux module, which also has a value of jorge:

<Scene
    key="sceneTwo"
    component={componentItem}
    title={content.title}
    item="jorge"
 />

I'd like to be able to do this:

console.log(this.props.item);  //output: 'This is some output'

Whereas in reality, I get this:

console.log(this.props.item);  //output: 'jorge'

I'm not sure what this technique is called hence the extremely vague title, but if anyone can suggest how I'd tackle this I'd be very grateful!

Upvotes: 0

Views: 104

Answers (2)

Gene Stempel
Gene Stempel

Reputation: 285

Assuming that variable jorje is within the scope of the of the usage of eval, this should work:

 console.log(eval(this.props.item))

Here is a sample use of eval with an abbreviated object containing your values:

const jorje = "This is some output";

var scene = {key:'sceneTwo', item:'jorje'};

console.log( scene.item ); // outputs jorje

console.log( eval( scene.item ));  //outputs This is some output

Upvotes: -1

Alex Young
Alex Young

Reputation: 4039

Your are passing the string "jorge" rather than the variable jorge. To pass a variable you need to wrap it in curly brackets:

<Scene
    key="sceneTwo"
    component={componentItem}
    title={content.title}
    item={jorge}
 />

Upvotes: 2

Related Questions