0

So I have an object. And the object has 6 variables (a, b, c, d, e, f)

Something like this:

let obj={
   a:0,
   b:0,
   ...
   ...
   }

I am passing this object as a function parameter and the function has to set the values of all the variables (a to f). The values are simply copied from other variables (lets call them u, v, w, x, y, z).

Based on the user's (my own) choice, a could be mapped to w or y or z etc and so on. Currently I am doing this manually like this:

   obj.a=x;
   obj.b=y;
   obj.c=w;
   ...
   ...

The issue is, I want this mapping pattern to be very flexible. I want to change the entire mapping with the least change of coding and I want the coding to be as minimal/clean as possible. Currently there are 4 possible mapping options based on how I want to do it at runtime. But I want to add more options and as mentioned above, I want the mappings to be very change-friendly.

Should I declare a mapping string and use eval to automate the mapping? Then I would have to simply change the string arrangement and the values will be mapped automatically. Something like this:

   var mapping="xyzuvw", values="abcdef";
   for(var i=0;i<6;++i)  //using 6 since mapping string is of 6 chars
      eval("obj."+values[i]+"="+mapping[i]);

As you can see, I only have to change (or create a new) mapping string and all the mapping sequence is automated.

The software is intended entirely for my own/private use.

Should I use eval() to automate this mapping process or should I manually edit (and create new) the mapping coding if I want to change the mapping pattern?

As in, will using eval() make the code prone to processing hiccups, runtime errors etc?

Will maintaining the code be easier in future with eval() or with manual mapping?


Edit to add:

For members who are referring me to Dynamically access object property using variabl, please note that I am trying to automate target variable name (whose value we want to copy) and not base variable name (whose value we want to change). As in, I want to automate whether obj.a = x; or whether obj.a=y;

Thanks.

Youstay Igo
  • 321
  • 2
  • 11
  • You don't need `eval` here. Just use `obj[values[i]] = mapping[i]`. – Ivar Sep 24 '21 at 13:27
  • You don't need to use eval on object properties,. `object[somevar] = somevalue` – Keith Sep 24 '21 at 13:29
  • @Ivar the difference is that the target variable name (to whose value we need to assign our object variable) is dynamic here. The question you referred to, is trying to dynamically access object variables. – Youstay Igo Sep 24 '21 at 13:35
  • @Keith Of course I can manually change the object variables. That is what I am doing currently. The issue (as I have mentioned in my problem statement) is that there are multiple if-else based assignment blocks based on which mapping I want to use at runtime. The question is not whether I **need** to use eval() on object variables, rather whether I **should** do it for convenience and change-friendliness sake? – Youstay Igo Sep 24 '21 at 13:38
  • 2
    I only see you assign properties to an object dynamically, not variables. `eval("obj."+values[i]+"="+mapping[i])` should have the same result as `obj[values[i]] = mapping[i]`. – Ivar Sep 24 '21 at 13:40
  • Still don't get what your problem is, somevar in my example is dynamic by nature. Using eval doesn't make it more dynamic. – Keith Sep 24 '21 at 13:40
  • @Ivar bro/sis mapping[i] contains the NAME of the variable we want to copy and not the value itself. As in, if mapping[i] is "myvalue1" then obj.a=myvalue1; If mapping[i] is "othervaulue" then obj.a=myvalue; etc. – Youstay Igo Sep 24 '21 at 13:47
  • @Keith I am trying to automate the variable name I want to copy from (this could be any name, such as x, y, z etc). I don't want to automate the object variable which I am trying to assign to. – Youstay Igo Sep 24 '21 at 13:50
  • @YoustayIgo I see. In that case ["Variable" variables in JavaScript](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript) would be more appropriate. Store those values into an object so you can dynamically access them. [Like so](https://jsfiddle.net/9bgLpzku/). – Ivar Sep 24 '21 at 13:53
  • @Ivar owh. This would work indeed. Except that every time I want to change the mapping, I would have to individually change the variable values to which I am pointing at. The mapping sequence itself would remain the same. As in, this is basically the same thing I am currently doing, just more ... fancy. – Youstay Igo Sep 24 '21 at 13:59
  • Using eval to access var's is certainly possible, but like mentioned by @Ivar using another object to store your mappings would be much cleaner if your able to restructure your code to do this. One issue with eval here is scope, so it would be easy to break, but that's a choice you need to take. – Keith Sep 24 '21 at 14:09
  • Can you guys post your suggestions as answer(s)? – Youstay Igo Sep 24 '21 at 14:14

0 Answers0