0

I have a big array called MyArray. The size of this big array will not exceed 250 items.(But it can be less). How i can copy data of this big array to smaller chunks size of 50 items per array.(I want to have 5 smaller size array of size 50 each and original array is untouched)

could any one show my how this can done?Thanks in Advance.

var MyArray = new Array();
var SmallArray1 = new Array();
var SmallArray2 = new Array();
var SmallArray3 = new Array();
Var SmallArray4 = new Array();
Var SmallArray5 = new Array();

My Big Array Structure:

MyArray.push({ url: urlValue, filename: NewFileName });
user1788736
  • 2,727
  • 20
  • 66
  • 110

2 Answers2

1

This example should help you:

var MyArray = new Array();
MyArray.push({url: 'url1', filename: 'fn1'});
MyArray.push({url: 'url2', filename: 'fn2'});
MyArray.push({url: 'url3', filename: 'fn3'});
MyArray.push({url: 'url4', filename: 'fn4'});

var SmallArray = MyArray.slice(1, 3);

alert(SmallArray[0].url); // url2
alert(SmallArray[1].url); // url3

In your case the code will be something like as follows:

var SmallArray1 = MyArray.slice(0, 50);
var SmallArray2 = MyArray.slice(50, 100);
var SmallArray3 = MyArray.slice(100, 150);
var SmallArray4 = MyArray.slice(150, 200);
var SmallArray5 = MyArray.slice(200, 250);

Note that you have 250 elements indexed from 0 to 249. First argument of .slice() method is index at which to begin, second argument is index at which to end, up to but not including.

omegastripes
  • 12,351
  • 4
  • 45
  • 96
  • 1 and 2 in slice(1,2) is start and end of array position ? so i should do like this :var SmallArray1 = MyArray.slice(0, 50); and var SmallArray2 = MyArray.slice(50, 100); and var SmallArray3 = MyArray.slice(100, 150); and so on ? – user1788736 Jun 16 '15 at 23:44
  • Yes, first and second arguments are the first and the last indexes in array to be sliced. [Here](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) you can find a description. – omegastripes Jun 16 '15 at 23:47
  • 1
    I added the example. – omegastripes Jun 16 '15 at 23:53
  • Many thanks for example. One question. Suppose if i have 240 in my original array. Using var SmallArray5 = MyArray.slice(200, 249); will copy that last 40 items into smallArray5 correctly or there will be error since i don't have exactly 50 items at the end ? – user1788736 Jun 16 '15 at 23:57
  • `.slice()` will copy existing elements without error even if you are either referring out of range or empty array. For latter it will give you an empty array. – omegastripes Jun 17 '15 at 00:25
  • may i know why alert('smallArray Total Item:'+SmallerArray1.length); gives me 49 instead of 50 ? – user1788736 Jun 17 '15 at 00:37
  • Oh my bad! @user1788736, apologies, you were right, and I got you mystified. See my fixed answer above. – omegastripes Jun 17 '15 at 19:43
-1

Can't you just assign the components of the big array into smaller arrays in for loops?

for (i=0;i<51;i++)
{
     bigArray[i] = smallerArray1[i];
}
for (j=51; j<101;j++)
{
     bigArray[i] = smallerArray2[counter];
     counter++;
}
for (k=101; k<151;k++)
{
    bigArray[k] = smallerArray3[counter2];
    counter2++;
}