Skip to content
Advertisement

How to attach multiple class method from loop?

.regularExpression() is hardcoded 3 times in the script, is there a way to dynamically attach it from loops from variable const regex = ['reg1', 'reg2', 'reg3']?

Usage:

JavaScript

Advertisement

Answer

You can use the reduce method:

JavaScript

In this way, starting from the initial value Check.string(), you will iteratively chain new regular expressions, accordingly to your array.

Alternatively, you can use a plain for-loop:

JavaScript
Advertisement