0

I have data in array

var dataArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... , 998, 999, 1000];

if i want to select data to show 10 times in each time I want to show 100 data in arrays

First time show output

1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , ..., 100

Second time show output

101, 102, 103, 104, 105 , ..., 200

.

.

.

tenth time show output

901, 902, 903, 904, 905, ..., 1000

Which function can help me or other way can help me (Node Js)?

Aidento
  • 35
  • 1
  • 7
  • Possible duplicate of [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – noahnu Jun 12 '17 at 03:23

3 Answers3

1

please try this code and feel free to adapt it according to your NodeJS requirements.

Best,

//Init array
var initArray = function(count){
  var res = [];
  for (var i = 0; i < count; i++) {
    res.push(i+1);
  }
  return res;
};

var myArray = initArray(1000);


var idx = 0, cutVal = 100;

var showMe = function(){
  var print = '';
  for (var i = idx; i < (idx + cutVal); i++) {
    print = print + myArray[i] + ', ';
  }
  console.log(print);
  idx = idx + cutVal;
};
showMe();
showMe();
showMe();
Dody
  • 608
  • 7
  • 19
1

let sidx = 0;
let size = 100;
let results = [];
function divide() {
    for(let i = sidx; i < (sidx + size) && i < dataArray.length; i++){
        results.push(data[i]);
    }
    sidx += size;
    //showData of result
    process.nextTick(divide);
}
or you can use setTimeout instead of process.nextTick
Borkes
  • 181
  • 1
  • 2
  • 11
0

const p = (arr, n) => {
  let portion, start = 0;
  while ((portion = arr.slice(start, start + n)).length) {
    console.log(portion.join(','));
    start += n;
  }
};

const inputA = Object.keys([...Array(30)]);
p(inputA, 10);
noahnu
  • 3,479
  • 2
  • 18
  • 40