What is the best way to split an array into four columns and allocate 10 <li> items from the array to each column?

If I have a dynamic array with 40 elements that changes on every render, how can I loop through the array and return 4 groups of elements, each containing 10 items without any repetition? I want to style these objects in the array using a parent flex container with 4 div elements set to flex:25%, and the li elements as children. How can I achieve this using JavaScript when the images are not static?

Answer №1

JavaScript provides a solution for this task.

var myArray = [];
var i = 0;
for(i=0;i<40;i++){
  myArray.push("Element "+i);
}

var obj = document.getElementById("temp_main");
counter = 10;
firstElement = true;
output = '';

for(i=0;i<40;i++){
  var temp = '<div class="row">' + myArray[i] + '</div>';
  if(counter>=10){
    temp = '<div class="column">' + temp;
    if( !firstElement ) temp = '</div>' +  temp;
    counter = 0;
  }
  output += temp;
  firstElement = false;
  counter++;
}

output += '</div>'; //closing last column
obj.innerHTML = output;
.row{
  display:block;
  box-sizing:border-box;
  padding:10;
  padding-right:5;
  width:100%;
}

.column{
  display:block;
  box-sizing:border-box;
  padding:0px;
  width:25%;
  border:1px red solid;
  float:left;
  clear:none;
}

#temp_main{
  display:block;
  width:100%;
}
<div id="temp_main">

</div>

I trust that you find this information beneficial!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm having trouble getting the Tanstack/react-query library to function properly within my Vercel application

Exploring the packaging and troubleshooting process... "@tanstack/react-query": "^5.14.2", "@tanstack/react-query-devtools": "^5.14.2", "next": "^13.5.6", "react": "18.2.0", & ...

The combination of Material UI nested theme providers can disrupt the functionality of the withStyles H

My React application was developed using Create React App and incorporates the @material-ui/core npm package for theming. To customize components, I make use of the withStyles higher-order component provided by MaterialUI. The documentation mentions supp ...

Is getDerivedStateFromProps blocked by shouldComponentUpdate?

I'm in the process of updating an older component that currently uses: shouldComponentUpdate() to prevent unnecessary state re-computation componentWillUpdate() to perform the re-computation and render only if 1 is true In the documentation, it sta ...

CSS trick that works on iOS16 as well

Previously, this CSS workaround was effective up to iOS version 15.6; however, it does not seem to be functioning in iOS 16. @media not all and (min-resolution:.001dpcm) { @supports (-webkit-appearance:none) {} } Are there any updated techniques avail ...

Highlight.js is not able to display HTML code

It's strange, I can't seem to get the HTML code to display correctly. This is my HTML: <head> <link rel="stylesheet" href="/path/to/default.css"> <script src="/path/to/highlight.pack.js"></script> <script& ...

Combining the lower margin of an image with the padding of a container: a step-by-step guide

There are two divs with a 50px padding, making them 100px apart. The top div contains an image floated to the right with paragraphs wrapping around it. The requirements team requests a 20px margin below the image if text flows under it, but no margin if th ...

Adjust the color of the text for the items in the drop-down field on the WooCommerce

https://i.stack.imgur.com/CHJUz.png It appears that the font color on my main website is white. However, on the WooCommerce Checkout Page, all drop down fields' items are also displayed in white, making it difficult for users to see the options witho ...

Responsive Input Navigation Bar CSS

How can I create a top bar that resembles the design of Google Play? I am struggling to make the input box flexible and fit the window size... The minimum width of Div.search is set to 250px but it's not working. Can someone help? *{ box-sizing: ...

Setting all array elements to zero in C# without employing for loops

In C#, there is a shortcut to initialize an array called Buffer with all elements set to 0 without using a for loop. You can simply use the following one-liner: byte[] Buffer = Enumerable.Repeat((byte)0, 50).ToArray(); ...

"Troubleshoot: jQuery UI accordion not functioning properly in response to

I've encountered an issue with the accordion functionality while working with dynamic data. To address this, I'm incorporating JavaScript to generate <div> and <h3> tags for the accordion. Here is the code snippet I am using: $(&ap ...

Storing user input in MongoDB after encoding

I am currently exploring the most effective methods for storing and presenting user input in MongoDB. In traditional SQL databases, it is necessary to encode all user input as a precaution against injection attacks. However, in the context of MongoDB, ther ...

"Utilize the style attribute to modify the appearance based on the

I was the original asker of this question, but I failed to provide a clear description which led to not getting an answer. However, I will now explain everything here. Essentially, I am looking for a JavaScript function that can identify a class with a spe ...

Flatten a multidimensional array in PHP while preserving keys

I have written some PHP code to organize the children of an array using a recursive function. While everything is displayed correctly in one column, some arrays are missing the "Name" key. Does anyone know how I can ensure that all elements display their ...

Add a directive on the fly, establish a connection, and display it dynamically

Within my markup, I have a tag element called popup-window which is handled by a specific directive. If I wish to incorporate multiple similar widgets that can be displayed or hidden in various locations, I currently need to include all these elements dire ...

The first item in Swiper is incorrectly displayed after the initial cycle, even though the data is accurate

Currently, I am incorporating the slider functionality in Vue using the Swiper framework. Although everything seems to be functioning properly, there is a minor issue that arises when filtering the data and completing the first cycle after scrolling. The f ...

Designing a captivating loading screen in Sencha Touch optimized for various mobile device resolutions

I am currently working with Sencha Touch 1.1 and I need my application to be compatible across various mobile platforms such as Android, iPhone, iPad, and Blackberry. One of the requirements is to have a splash screen at startup, which I have tried impleme ...

Learn the process of extracting an array of objects by utilizing an interface

Working with an array of objects containing a large amount of data can be challenging. Here's an example dataset with multiple key-value pairs: [{ "id": 1, "name":"name1", age: 11, "skl": {"name": & ...

What is the best way to extract a value from two different HTML identifiers?

Something tells me that this task is quite simple. I just need some guidance on what I might be missing here. All I really want is a basic program that can extract the content from <span id "text"> and paste it into <div id="text2">. funct ...

The nested navigation link disappears suddenly on Internet Explorer 9

This query closely resembles another issue: Nested menu keeps vanishing on IE 8 & 9. However, the solution provided is quite limited. Why do my nested menus disappear before I can reach them with my cursor unless I move it swiftly? This peculiar behavi ...

Error in Jquery validation caused by incorrect file extension type

Within my HTML form, I have multiple inputs set up for validation purposes: <form role="form" id="addForm" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="userName">U ...