I have a large list that I am creating in Javascript and some items have sub items.
My list is something like this:
Let lista = [{
nombre: "Elveramiro",
direccion: [{
nombre: "Asunción Paraguay",
geo: "https://maps.google.com/?-25.282005, -57.635117"
}]
}]
The way I call the items, and it works fine; it is:
<span>${item.nombre}</span>
But I can't manage to call the item content direccion
separately. Because the name I show as text, and the geo I show in a link:
<span><a href="${item[direccion.lugar]}">${item[direccion.nombre}</a></span>
The complete structure I use is the following:
Let lista = [
{
nombre: "Elveramiro",
direccion: [{
nombre: "Asunción Paraguay",
geo: "https://maps.google.com/?-25.282005, -57.635117"
}]
}
]
Let tarjeta = "";
lista.forEach(item => {
tarjeta += `
<section>
<div class="tarjeta">
<span><a href="${item[direccion.lugar]}">${item[direccion.nombre}</a></span>
</div>
</section>
`;
});
document.getElementById("lista").innerHTML = tarjeta;
We have that an address is composed of the properties
That an item is composed of the properties
And that your list is an array of items.
Since names are important in this profession, I'm going to change your list. Instead of '
nombre
' I will identify the items by a 'grupo
' property. Your address array will conveniently be called 'direcciones
'.To work with this arrangement, a main loop is made that goes through the list. On each iteration, you declare a nested loop over the addresses of the current item.
I leave an example working
item is your object, so you can call address like item.address or item["address"] but address is also an array of objects, so to access the value, you either iterate through each one and then get the value, or get the value by position, type item.address[0].name
either
Example: