Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  152] [ 5]  / answers: 1 / hits: 19460  / 11 Years ago, wed, september 25, 2013, 12:00:00

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.


More From » arrays

 Answers
5

The following should work:



for (var i = 0; all.length !== 0; i++) {
var j = 0;
while (j < all.length) {
if (i >= all[j].length) {
all.splice(j, 1);
} else {
mix.push(all[j][i]);
j += 1;
}
}
}


On each iteration of the outer loop we increase i by one, this will be the index in each array to grab an item from. For the inner loop we will do one of the following:




  • If the index i is beyond the maximum index for the array all[j] we are done with that array so it is removed using all.splice(j, 1). We do not advance j because all[j] will refer to the next array after the previous element at that location was removed.

  • Otherwise we add the item all[j][i] to mix and increase j by one to move to the next array on the next iteration.



The outer loop doesn't stop until there are no arrays left in all, which will happen when i has exceeded the length of the longest array.



For example with three arrays all of different lengths:



var one = [1, 2, 3, 4];
var two = ['a', 'b'];
var three = ['U', 'V', 'W', 'X', 'Y', 'Z'];
var all = [one, two, three];
var mix = [];
// after running the above loop mix will have the following contents:
// [1, a, U, 2, b, V, 3, W, 4, X, Y, Z]

[#75434] Tuesday, September 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
louiseb

Total Points: 368
Total Questions: 107
Total Answers: 107

Location: Tanzania
Member since Wed, Feb 24, 2021
3 Years ago
;