Display loop values and display additional data in Javascript

admin Avatar

Here’s an example of using forEach() to display data from an array and every 10 elements displaying some additional data:

javascriptCopy codeconst dataArray = [...]; // replace this with your array

dataArray.forEach((data, index) => {
  if ((index + 1) % 10 === 0) {
    console.log("Displaying some additional data...");
    // add code here to display additional data every 10 elements

In the example above, dataArray represents your array of data. The forEach() method is used to iterate over each element of the array. The callback function passed to forEach() takes two arguments: data represents the current element of the array being processed, and index represents the index of the current element.

Inside the callback function, console.log() is used to display the current element of the array. The if statement checks whether the current index plus one is divisible by 10 without remainder, which means that we have reached a multiple of 10 (i.e., 10, 20, 30, etc.). If this condition is true, some additional data can be displayed.

Note that you’ll need to replace the ... in the first line with your actual array of data. Also, you can replace console.log() with any other code that you want to execute for each element of the array.

Tagged in :

admin Avatar

More Articles & Posts