Is there a way to perform a for within a literal template in js?
I retrieve a JSON and access them in a first for and everything goes well, but then the Permissions key has an array value, so I wanted to know if it can be done, let's say something like this:
for (let fila of r) {
tablaNotas.appendChild(stringToRowNode(
`<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-parent="#accordion" data-toggle="collapse" href="#${fila["IdUsuario"]}${fila["IdPermiso"]}" aria-expanded="false" aria-controls="${fila["IdUsuario"]}${fila["IdPermiso"]}">
${ fila["Email"].toString().toUpperCase()}
</a>
</h4>
<button type="button" class="collpase-button collapsed" data-parent="#accordion" data-toggle="collapse" href="#${fila["IdUsuario"]}${fila["IdPermiso"]}"></button>
</div>
<div class="panel-collapse collapse" role="tabpanel" id="${fila["IdUsuario"]}${fila["IdPermiso"]}">
<div class="panel-body">
for(let ${p} of ${ fila["Permisos"] }) {
<p>${ p["IdPermiso"] }</p>
<p>${ p["Nombre"] }</p>
}
</div>
</div>
</div>`
));
}
That's what I try to do.
Is this possible or is there a function that allows me to do something similar?
Not the way you want, but in a template string you can put any code in between
${ }
that returns something. So you could create the following:So your for loop
could be transformed into:
You could do it this way.
Where you first loop through the array and add the corresponding rows, then insert the results into your template.