DEV Community

Cover image for πŸ”„πŸ”‚ 5 Simple Ways to Loop Over Data in HTML Using JavaScript πŸ“ŠπŸ“
Muhammad Ayoub Khan
Muhammad Ayoub Khan

Posted on • Updated on

πŸ”„πŸ”‚ 5 Simple Ways to Loop Over Data in HTML Using JavaScript πŸ“ŠπŸ“

Looping over data is a common task when working with web applications. Whether you're displaying data in a table, a list, or any other format, you'll often need to loop through an array or object to generate the corresponding HTML markup. In this article, we'll explore five different ways to loop over data in HTML using JavaScript.

The for Loop

The for loop is the most common and versatile way to loop over an array in JavaScript. Here's an example:

const data = ['apple', 'banana', 'cherry'];

for (let i = 0; i < data.length; i++) {
  const listItem = document.createElement('li');
  listItem.textContent = data[i];
  document.querySelector('ul').appendChild(listItem);
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using a for loop to iterate over an array of fruits and create a new list item element for each one.

The forEach Method

The forEach method is a built-in function of arrays that allows you to loop through each element and perform an action on it. Here's an example:

const data = ['apple', 'banana', 'cherry'];

data.forEach((item) => {
  const listItem = document.createElement('li');
  listItem.textContent = item;
  document.querySelector('ul').appendChild(listItem);
});
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the forEach method to iterate over the array and create a new list item element for each element.

The for...in Loop

The for...in loop is a built-in JavaScript loop that allows you to loop over the keys of an object. Here's an example:

const data = { name: 'John', age: 30, city: 'New York' };

for (let key in data) {
  const listItem = document.createElement('li');
  listItem.textContent = `${key}: ${data[key]}`;
  document.querySelector('ul').appendChild(listItem);
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the for...in loop to loop over the keys of an object and create a new list item element for each key-value pair.

The for...of Loop

The for...of loop is a built-in JavaScript loop that allows you to loop over the values of an iterable object, such as an array. Here's an example:

const data = ['apple', 'banana', 'cherry'];

for (let item of data) {
  const listItem = document.createElement('li');
  listItem.textContent = item;
  document.querySelector('ul').appendChild(listItem);
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the for...of loop to loop over the values of an array and create a new list item element for each element.

The map Method

The map method is another built-in function of arrays that allows you to loop through each element and create a new array with the results. Here's an example:

const data = ['apple', 'banana', 'cherry'];

const listItems = data.map((item) => {
  const listItem = document.createElement('li');
  listItem.textContent = item;
  return listItem;
});
Enter fullscreen mode Exit fullscreen mode
document.querySelector('ul').append(...listItems);
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the map method to iterate over the array and create a new list item element for each element. We're then using the spread operator to append all of the list items to the unordered list element.

These are just a few of the many ways to loop over data in HTML using JavaScript. By understanding these techniques, you can write more efficient and maintainable code when working with web applications.

Top comments (0)