J. Doe J. Doe - 4 months ago 24
Javascript Question

How to use a function to reset values of a set of flags except the one used as its argument?

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: (

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)


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


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.