0

Without using eval ... I'm trying to see if I can get a value from a variable where the value is stored in another variable. Such as get value of 'ValueC' from varA where varA='varB' and varB='ValueC'

The user will enter a string, simulated below as the variable named 'UserSpecified'. They could enter any character and if doesn't match an existing variable ... well I'll just return null.

I realize I can do this with an eval(), but evals seem to be frowned upon I'm trying to do it in a better way.

I've tried the ES2015+ accepted answer from "Variable" variables in Javascript? but I'm not having any success. Am I missing something or should I stick with an eval?

let a  = "aye";
let b = "be";
let c = "sea";

let userSpecified="a";
let obj = {userSpecified};
let parm='userSpecified';

console.log(eval(userSpecified));  // This displays 'aye'

console.log(obj[parm]); // This displays 'a' but I would like it to display 'aye'

5
  • 3
    Why should it output aye when obj is { "userSpecified": "a" }? Commented Aug 1, 2019 at 16:44
  • Not sure if this is what you wanted to do but try: let userSpecified={a}; it should output both 'aye' as object of course. Commented Aug 1, 2019 at 16:52
  • Good question, sorry I should have explained that a bit. The user will enter a value in an edit field so I was trying to convey that with the variable named 'UserSpecified' but I obviously didn't do that well. I'm going to edit my question to clarify that point. Commented Aug 1, 2019 at 17:05
  • This still doesn't make much sense... Why obj? window[userSpecified] === "aye" // if userspecified === "a" Commented Aug 1, 2019 at 17:19
  • Because @DBTake3 is still expecting that value to be the name of a local variable. Commented Aug 1, 2019 at 17:22

2 Answers 2

1

If you can restructure your code so that an object holds the values, then it's just basic obj.key = val syntax (exactly: obj[key] = val).

const obj = {
  a: "aye",
  b: "be",
  c: "sea"
}

const userSpecified = "a";

console.log(obj[userSpecified]); // expected: aye

If not, then use var instead of let:

// pay attention that it's var not let
var a = "aye";

let userSpecified = "a";

console.log(window[userSpecified]) // expected: aye

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

2 Comments

Thanks, this helped.
I’m happy that I could help :)
0

It is not possible to get the value of a local variable dynamically (aside from eval).

But you can use an object to store data (not the names of local variables), and access that data with dynamic keys.

I think this is what you want:

const data = {
  a: "1",
  b: "2",
  c: "3",
}

// Take the name of an object property and return its corresponding value.
function getValue(userSpecified) {
  return data[userSpecified]
}

console.log(getValue("a")) // 1
console.log(getValue("b")) // 2

console.log(getValue(someFormField.value))

1 Comment

Thanks! This helped

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.