I have an array with several objects which I want to display in a container; I do have a notion of how to display/print the content of each object on the screen, however I have problems since I am trying to build a chain to pass to the container through the innerHTML()
.
I already have the structure of everything and I can visualize each object in the console; the following is what i have in javascript.
let rela = JSON.parse(localStorage.getItem("relacionados"));
let html = ' ';
for (let i = 0; i < rela.length; i++) {
console.log(rela[i]);
//*******
// html += '<div class="item"><img class="trj_img_cls" src="${rela[i].foto}" alt="${rela[i].nombre}" id="${rela[]i.categoria} . , . ${rela[i].subcategoria}';
//*******
}
//*******
// document.getElementById('contenedorR').innerHTML = html;
//*******
The problem is inside the loop when assembling the content of the html variable since I have tried to use ${...}
, that is, the symbol " " seguido de los corchetes pero y sin obviar que debo finalizar la cadena antes de que desee usar lo ya mencionado y luego de haber ocupado el "'
${...}`'" but in the editor the whole variable is shown to me as a single string (as if it hadn't finished and started with the '; also another problem gives me when assembling the id attribute since, as you can see, it doesn't only have one element.
Does anyone know how I can solve this problem so that it can be added later with the innerHTML()
?
I would also like to know how the can be used .toFixed()
in the part of the id attribute in case it is required to display a value with 2 decimal places.
The solution I found was that in the html variable inside the loop I matched it with backtiks to the single quotes I deleted.
the code was as follows:
I was able to do this by reading an article about backtiks; the link is: https://parzibyte.me/blog/2018/03/26/backticks-javascript-templates-strings/
What I still haven't managed to get working for me as well is the
.toFixed()
in case of increment on the "id" attribute${rela[i].valor}
, try:results:
it appeared in console 99.toFixed(2)
no value or element of the array was returned
I'm trying too
number_format()