I am trying to concatenate an array of strings:
const funcNames = ['paciente.obtener', 'paciente.buscar', 'paciente.contiene']
This arrangement can continue to grow...
Each one represents a function, for example: patient.obtain('expression') What I want to do is go through the list of strings and if the element has another element in front of its list, put it inside the function and if it is the last one, leave it as patient.contains(expression)
For example, this list would look like this:
paciente.obtener(paciente.buscar(paciente.contiene('expresion')))
I am trying this way:
funcNames.forEach((e, i) => {
const demo = funcNames[i + 1] ? `${e}(${funcNames[i + 1]}(expresión))` : `${e}(expresión)`;
console.log(demo);
});
but this only works with 2 and I don't know how to concatenate with the others so that the "function" grows
I suggest you use an
for
invert and go adding to a temporary or executing one by one otherwise, here is an example:Taking the same idea from @JhoubertRincon of iterating from the end, we can use
.reduceRigth()
: