0

Goal and approach:

I have a set of flags having integer values (either 1 or 2). I store them in a "global" array var myArray = [] and later: (myArray.push(flag1) ... myArray.push(flagn)).

I want to create a function to which I could pass one of these flags so that it resets the values of all other flags to 1 except the argument flag which must be reset to 2.

I have hard time to achieve this because I need to compare the name of the argument flag to the names of the array of flags.

Alternative (non working):

After some research on this website (How to convert variable name to string in JavaScript?), I had to change the array to an object

But then I faced an other problem: there is aparently no way to get the name of the argument passed to a function (Determine original name of variable after its passed to a function)

Question:

So basically my approach to reach my goal can not work. Could you please suggest me an approach to fulfill my goal?

Community
  • 1
  • 1
J. Doe
  • 85
  • 7
  • 1
    Please provide a concrete example of input and desired output. – trincot Oct 29 '16 at 15:07
  • Just pass the name of the argument as an additional parameter to your function. – jfriend00 Oct 29 '16 at 15:08
  • @jfriend00 Amazing idea ! Thank you very much. If you convert your comment to an answer I will accept it (without testing, I know it will work for sure) – J. Doe Oct 29 '16 at 15:10

1 Answers1

1

You can just pass the name of the argument as an additional parameter to your function. Then, you will have both value and property name as arguments and you should be able to solve your problem.

jfriend00
  • 683,504
  • 96
  • 985
  • 979