'Iterate through array 1000 items at a time in an array of thousands
I want to loop through an array consisting of thousands of items. I want to process thousand item at a time. Is there a better way to achieve this ?
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let multiplier = 0
let set= 2
for(let i = 0; i< a.length; i+=set) {
let p = set * multiplier
let q = (set * multiplier) + set
multiplier += 1
const eachSet = a.slice(p, q)
await API.post("/test", eachSet)
}
Solution 1:[1]
Technically no: everything on a webpage ultimately has to come from that webpage, so there's no magic way to add <script>
tags to the HTML ... except to actually add them.
Generally sites solve this by having a single <script>
tag for their main "package" of Javascript, and then they have that package bring in all the others that the site depends on. There are numerous libraries for doing this sort of "packaging" (Webpack being the current most popular).
However, you still do need that initial script tag even with such bundlers. Another solution many sites have to this is to have a "single page" site, which uses only one HTML page, and then they use Javascript to simulate "virtual pages" on the site. React with React Router would be an example of this.
But if you already have a large mess of HTML files, you either need to add that tag manually, or have a computer add it for you. Assuming you don't want to do it by hand, you really have two options in that case.
The first would be to get a proper webserver, one that can add the script tag to each file, as it serves it. The second (which I'm guessing will be easier) would be to just write a simple command line script to append a string (of that script tag) to the end of every file.
I'm not sure how familiar you are with Node.js (ie. Javascript outside the browser), but you could pretty easily whip something like that up using Node's fs
(filesystem) module.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |