Modelesq Modelesq - 2 months ago 8
Javascript Question

Add to array for every 60 counted from 1000+ results in javascript

What I'm trying to do is create a page for every 60 results found. Meaning if I'm given a number

1,390
results
For every 60 of those results, add to array. Meaning, the results array will hold page numbers.

Expected result array:

results = [1,2,3,4,5...]


I'm trying to go for the best memory saving tactic, but maybe it seems a loop is most appropriate?

results = [];
for (var i = 0, res = 0; i < total; i + 60, res++) {
results.push(res);
}
// this just causes browser to crash


How can I add a result to the result array for every 60 results?

Answer

var total = 1390;
var numPages = Math.ceil(total / 60);
var results = [];
for (i = 1; i <= numPages; i++) {
    results.push(i);
}

console.log(JSON.stringify(results));