What is the best way to divide a large array into smaller arrays using JavaScript?

I have a large array that I need to divide into smaller arrays in order to improve performance. The current array looks like this:

var callUrls = ['url1','url2','url3','url4','url5','url6','url7','url8','url9','url10'];

I would like to split it into two arrays like this:

var callUrls = [
    [url1, url2, url3, url4, url5],
    [url6, url7, url8, url9, url10]
];

Can anyone provide guidance on how to achieve this in JavaScript? Thank you in advance.

Answer №1

Check out this example:

const urlsToCall = ['url1','url2','url3','url4','url5','url6','url7','url8','url9','url10'];
const n = 5;
const temporaryArray = [];
for(let i=0; i<urlsToCall.length; i+=n)
  temporaryArray.push(urlsToCall.slice(i, i+n));
console.log(temporaryArray);

JSFIDDLE

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

Is there a way to access the edit section without having to reload the page for every post in Django using Javascript?

This snippet represents my HTML template: <div class="posts"> <h3> <a href ="{% url 'profile' p.user.id %}"> {{p.user}}: </a> </h3> <p>{{p.timestamp}}</p> <br> <p>< ...

Guide on transferring Vue form input data to Adonis controller through an http request

I have a Vue component that prompts the user to enter their email address. I've utilized v-model for data binding. <template> <input v-model="email" type="text" placeholder="Email" /> <button class="tiny">Send</button> ...

What is the best way to toggle the expansion and collapse of a div created with the aria-expanded attribute

I've been working on a script to expand a collapsed <div> element using JavaScript. While I can easily collapse and expand the <div> interactively on the frontend, I ran into issues when trying to do so programmatically with JavaScript. Th ...

What separates an unregistered HTML Custom Element from a registered one?

If I forget to register a custom element, I can still: customize the unregistered element using CSS attach events to the unregistered element using JS For example: // REGISTER <my-custom-element-1> class MyRegisteredCustomElement1 extends HTMLEl ...

Easily add items to your shopping cart by dragging and dropping them and then triggering the preset onClick event with the push of

I am looking to enhance my webshop by incorporating a drag and drop feature. After successfully programming an "Add to Cart" button for each product using jQuery's onClick event $(".cartButton").click(function () { ... All the backend coding for th ...

What is the process for triggering a PHP function when a form element is clicked in a webpage?

Currently, I am trying to implement a jQuery colorbox on my webpage which appears over a <select> drop-down list. My goal is to trigger an AJAX call every time a new option is selected from the drop-down. Even though I have written the following cod ...

The onkeyup function is failing to recognize the Regular Expression

$(function(){ $('#addAction').click(function(){ count += 1; $('#extras').append('<div class="row"><div class="col-md-1">Delivery Fee: </div><input id="price_' + count + '" name="pr ...

Slice PHP arrays based on specific indices

I am trying to implement custom array slicing by specifying certain indexes on each function call. I have set the upper and lower limits of the array indexes to determine the range for slicing. In PHP, I am attempting to get different array slices with eac ...

What is the safest method for integrating a private MD5 key into a JavaScript algorithm?

Looking to incorporate online payments into my website using html and jquery. The simplest method seems to be sending a form to a specific link. One of the parameters required is a signature, which is a hash generated from form fields and a private key. Ho ...

Elements are randomly glitching out with CSS transitions in Firefox

Chrome is working perfectly for me, but when I switch to Firefox it behaves differently than expected I am attempting to create a simple animation (utilizing transitions) that continuously runs on mouseover and smoothly returns to the starting position on ...

Generating an <input> element with type="image" that navigates to a different aspx page with parameters

Provided with immense assistance from stackoverflow, as a budding programmer, I must attest that this platform has proven to be an invaluable resource! Currently, I am utilizing vb.net to dynamically generate a datatable. This datatable is then bound to a ...

Get the precise element from an array object in a MongoDB collection

I am new to working with MongoDB and I am trying to retrieve specific fields from an object array in a MongoDB collection. Can someone guide me on how to achieve this? Within my database, there is a structure named skills which is in the form of an array. ...

Break up the URL into parts and make substitutions

Here is the URL: /shop/rest/model/atg/commerce/catalog/ProductCatalogActor/getHardwareFamilySkus?skuId=sku1431389 I want to split the URL into two parts as shown below: abc => /shop/rest/model/atg/commerce/catalog/ProductCatalogActor/ pqr => getH ...

Comparison of performance in large 3D arrays: organized 1D storage versus T*** technology

I am seeking advice on how to efficiently store large 3D arrays (e.g., 2000 x 2000 x 2000) for finite difference discretization computations. Does using contiguous storage with float* offer better performance compared to float*** on modern CPU architecture ...

displaying solely the bottom two rows while concealing the remainder

Looking for a Google Docs script that can automatically display only the last two rows in a spreadsheet. The goal is to have the new data added in a row and when ENTER is pressed, it will show only the latest two rows while hiding all others. I've st ...

What is the best way to automatically submit a form with multiple fields once there are no active fields?

One of my tasks involves managing an Elementor form with multiple fields. I am trying to set up the form to automatically submit whenever there is no longer focus on any of the fields, such as clicking somewhere else. For a form that only has one field, m ...

Alternatives to using $.getJSON()

When utilizing jQuery within the React/Redux environment, what alternative library is typically used for handling straightforward REST calls instead of $.getJSON or $.postJSON? Is there a widely-used option that functions similarly to node's http mod ...

FSharp CSV - converting data in each row into an array of columns

I am currently facing a challenge with a CSV file I have. The first column contains titles, while the next 700+ columns consist of integer data. Title D1 D2 D3 D4 .. D700 Name1 0 1 7 5 48 I am attempting to utilize CsvProvider to read the file an ...

Node.js for Streaming Videos

I am currently working on streaming video using node.js and I recently came across this helpful article. The setup works perfectly when streaming from a local source, but now I need to stream the video from a web source. However, my specific requirement i ...

The range slider is malfunctioning in the audio player, JQuery, and HTML

I have been struggling with getting the Range slider to work on my custom-built mp3 player using jQuery. Despite trying various codes from different sources, I can't seem to get it to function correctly. Strangely enough, it works fine on Mozilla brow ...