How to avoid 'eval()'?

Having several objects (instances) of the same class, how do I go through all objects?
I made an array with all object names (property ‘.name’), but then I need the eval() command. (My program is a simulation of a railway interlocking.)

Can you post more of your code for context? Describe exactly what the code does now vs. what you want the code to do.

<script>

const mySignalNames = [];

class signal {
  constructor(a) {
    this.name = a;
    this.ok = true;
    mySignalNames.push(this.name);
  }
}

const C1 = new signal("C1");
const C2 = new signal("C2");

// during further process
C2.ok = false;

function resetSignals() {
    for (let sig of mySignalNames) {
		eval(sig + '.ok = true');
	}
}

// later on
resetSignals();

</script>

Why not store the actual signals in an array instead of just the names? This would allow you to iterate through the signals and change the ok property to true for each one.

Ups - of course - simple solution, thanks!
so:

<script>

const mySignals = [];

class signal {
  constructor(a) {
    this.name = a;
    this.ok = true;
    mySignals.push(this);
  }
}

const C1 = new signal("C1");
const C2 = new signal("C2");

// during further process
C2.ok = false;

function resetSignals() {
    for (let sig of mySignals) {
		sig.ok = true;
	}
}

// later on
resetSignals();

</script>

I personally would avoid modifying a global inside the class.

Instead, I would do:

const C1 = new signal("C1");
mySignals.push(C1)
const C2 = new signal("C2");
mySignals.push(C2);

Ok, didn’t know that modifying a global inside the class should be avoided.
So when I have a lot of signals, I will create the objects and fill mySignals in a loop, so I don’t have to write x times ‘mySignals.push(x)’.

Ideally, a class would only manipulate things that are directly apart of the class.

Will you really ever need to reference C1 or C2 explicitly?

Another approach to avoid globals and manipulating them would be to create an additional class.

class Signal {
  constructor() {
    this.ok = true;
  }

  change(val) {
    this.ok = val;
  }
}

class Railway {
  constructor() {
    this.signals = {};
  }

  addSignal(name) {
    this.signals[name] = new Signal();
  }

  changeSignal(name, val) {
    this.signals[name].change(val);
  }

  resetSignals() {
    for (const signal in this.signals) {
      this.signals[signal].ok = true;
    }
  }
}

const railway = new Railway();
railway.addSignal("C1");
railway.addSignal("C2");

railway.changeSignal("C2", false);

// later on
railway.resetSignals();
console.log(railway.signals);

Thanks a lot again - helpful! Further one, I’ll need all elements (tracks, signals, points etc) to reference, hence I adopted your second proposal.
In the following example, I need to reference i.e. Signal C2 having the property ‘fromTrack’ as a track object. I didn’t find how to find out the name of this object.
(In the console writing ‘elements.signals[‘C2’].fromTrack’ the output is ‘Track {ok: true}’, but I can’t find the name (it would be tr99).
Here my code: