Bit of a long winded title but I'll try to explain what I want to do.
Basically I've got a number of arrays that I want to combine into a single array. The trouble is, I need to loop through the items in the subarrays and add them 1 at a time and maintain the order. The end goal is I want to display the data back paged. I've got a simple example below that I will use to try and convey what I mean. This isn't an alphabetical sort i.e h should not be before i in the bottom example.
So in my example I know I want 3 pages of results. The first page should have 4 items, the second page 4 items and the third only 1 item.
I can do the final paging myself as I will have an array of all inner items at the end of it mix, but I can't work out how to loop through my arrays and add them in how I need.
I've got the page variable upfront but I'm not sure how to structure the loop. I think I basically need to loop through each subarray and pop() the first item off, then loop through the next one, pop() the first item and so forth. But somewhere I need to check how many items are left in each subarray.
For instance if I only had array one I would in theory have 2 pages the first containing a,c,e,i and the second only k, this one is obviously simple enough as I just check the length of the only array.
But if I added in another array third [1,2,3,4,5] then I would expect the mix array to be ['a','b',1,'c','d',2...etc]; Each of these arrays could in theory have different lengths so then I would obviously skip an empty value.
var one = ['a','c','e','i','k'];
var two = ['b','d','f','h'];
var all = [one,two];
var pagecount = 3;
var mix = [];
for(var i = 0; i< all.length; i++){
var area = all[i];
}
// End result should be mix = ['a','b','c','d','e','f','i','h','k'];
I've tried to word this as best as I can, but I'm struggling to get my head around how to explain this myself! Unfortunately in the real world I have no control over the data/size of the data arrays.
Any questions or if something is not clear then please leave a comment.