LISTAS HTML + JavaScript

Tiempo de lectura: 5 minutos

Reading time: 2 minutes

To create a simple list from an array of objects, you need to follow the steps below:

First, create a file with the .html extension, where we’ll create a div with an associated ID that will display the list of elements. The ID for the list, in this case, will be “lista”.

<div id="lista">

Once we have the ID where we will build the list, create a file with the .js extension. In this file, we’ll develop the logic part.

Create a file named main.js as an example, then associate this file with the HTML as follows:

Within the JavaScript file, declare and initialize the array of objects that we want to display in our list:

var objectArray = [
        {
            id: 1,
            name: "DevCodeLight1",
        },
        {
            id: 2,
            name: "DevCodeLight2",
            
        },
        {
            id: 2,
            name: "DevCodeLight2",
        }
    ];

Add the following method, which is triggered when the HTML document has been completely loaded and parsed:

document.addEventListener("DOMContentLoaded", function(event) {
  
});

Create a function to handle the logic for rendering our object list.

We’ll iterate over the data using a for loop, and for each element, we’ll perform the same rendering. In this case, we’ll access the attributes of each object, its ID and name. We’ll add the attributes inside a list, using the <li></li> tags.

Finally, outside the loop, specify the ID of the element where we want to display the data list.

function createObjectList() {
        var accumulator = "";
        for (var i = 0; i < objectArray.length; i++) {
            accumulator += "<li>" + objectArray[i].id + " Name: " + objectArray[i].name + "</li>";
        }
        document.getElementById("lista").innerHTML = accumulator;
    }

Call this method from the previously created function, resulting in the following JavaScript:

Finally, open the index.html file in a browser to see the result.

I hope this helps you out!

Continuing with the translation:

Reading time: 2 minutes

To create a simple list from an array of objects, you need to follow the steps below:

First, create a file with the .html extension, where we'll create a div with an associated ID that will display the list of elements. The ID for the list, in this case, will be "lista".

<div id="lista">

Once we have the ID where we will build the list, create a file with the .js extension. In this file, we'll develop the logic part.

Create a file named main.js as an example, then associate this file with the HTML as follows:

Within the JavaScript file, declare and initialize the array of objects that we want to display in our list:

var objectArray = [
    {
        id: 1,
        name: "DevCodeLight1",
    },
    {
        id: 2,
        name: "DevCodeLight2",
    },
    {
        id: 2,
        name: "DevCodeLight2",
    }
];

Add the following method, which is triggered when the HTML document has been completely loaded and parsed:

document.addEventListener("DOMContentLoaded", function(event) {
  
});

Create a function to handle the logic for rendering our object list.

We'll iterate over the data using a for loop, and for each element, we'll perform the same rendering. In this case, we'll access the attributes of each object, its ID and name. We'll add the attributes inside a list, using the <li></li> tags.

Finally, outside the loop, specify the ID of the element where we want to display the data list.

function createObjectList() {
    var accumulator = "";
    for (var i = 0; i < objectArray.length; i++) {
        accumulator += "<li>" + objectArray[i].id + " Name: " + objectArray[i].name + "</li>";
    }
    document.getElementById("lista").innerHTML = accumulator;
}

Call this method from the previously created function, resulting in the following JavaScript:

Finally, open the index.html file in a browser to see the result.

I hope this helps you out!

Continuing with the translation:

Reading time: 2 minutes

To create a simple list from an array of objects, you need to follow the steps below:

First, create a file with the .html extension, where we'll create a div with an associated ID that will display the list of elements. The ID for the list, in this case, will be "lista".

<div id="lista">

Once we have the ID where we will build the list, create a file with the .js extension. In this file, we'll develop the logic part.

Create a file named main.js as an example, then associate this file with the HTML as follows:

Within the JavaScript file, declare and initialize the array of objects that we want to display in our list:

var objectArray = [
    {
        id: 1,
        name: "DevCodeLight1",
    },
    {
        id: 2,
        name: "DevCodeLight2",
    },
    {
        id: 2,
        name: "DevCodeLight2",
    }
];

Add the following method, which is triggered when the HTML document has been completely loaded and parsed:

document.addEventListener("DOMContentLoaded", function(event) {
  
});

Create a function to handle the logic for rendering our object list.

We'll iterate over the data using a for loop, and for each element, we'll perform the same rendering. In this case, we'll access the attributes of each object, its ID and name. We'll add the attributes inside a list, using the <li></li> tags.

Finally, outside the loop, specify the ID of the element where we want to display the data list.

function createObjectList() {
    var accumulator = "";
    for (var i = 0; i < objectArray.length; i++) {
        accumulator += "<li>" + objectArray[i].id + " Name: " + objectArray[i].name + "</li>";
    }
    document.getElementById("lista").innerHTML = accumulator;
}

Call this method from the previously created function, resulting in the following JavaScript:

Finally, open the index.html file in a browser to see the result.

I hope this helps you out!

Leave a Comment