通过多个阵列循环,结合到单一阵列,子阵内维持指标 [英] Loop through multiple arrays, combining to single array,maintaining indexes within the subarray
问题描述
一个长篇大论称号一点,但我会尽力解释我想做的事情。
基本上,我有一个数字,我想合并成一个单一阵列阵列。麻烦的是,我需要遍历子阵中的项目,并将它们添加1的时间和维持秩序。最终目标是我要显示分页回数据。我有低于我会用尝试传达我的意思是一个简单的例子。这不是一个字母排序即 ^ h 不应该是之前的我在底部例如
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.
所以在我的例子中,我知道我要3页的结果。第一个页面都应该有4个项目,第二页4项和第三只有1项。
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.
我已经得到了页面变量前期,但我不知道如何构建循环。我想,我基本上都是通过各子阵列和pop()的第一个项目关闭,然后在接下来的一个循环,pop()方法的第一个项目,等等需要循环。但地方我需要检查多少个项目都留在每个子阵列。
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.
举例来说,如果我只有阵列一我会在理论上有2页第一个包含A,C,E,I和仅次于K,这一次显然是很简单,因为我只是检查的长度只有数组。
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.
但是,如果我在另一个阵列添加第三[1,2,3,4,5],然后我希望混合阵列为['A','B',1,'C','D ',2 ...等];每个阵列在理论上可以有不同的长度所以后来我显然会跳过空值。
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.
推荐答案
下面应该工作:
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;
}
}
}
在外环我们增加的每一次迭代 I
接一个,这将是每个阵列中的索引从抓项目。对于内环我们会做下列之一:
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:
- 如果该指数
I
超出了所有[J]
我们正在与做了最大的磁盘阵列指数数组,以便它使用all.splice删除(J,1)
。我们不提前Ĵ
,因为所有[J]
将指向下一个数组的previous元素在后该位置被移除了。 - ,否则我们添加的项目
所有的研究[J] [I]
到混合
和增加Ĵ
一个地移动到下一个迭代的下一个阵列。
- If the index
i
is beyond the maximum index for the arrayall[j]
we are done with that array so it is removed usingall.splice(j, 1)
. We do not advancej
becauseall[j]
will refer to the next array after the previous element at that location was removed. - Otherwise we add the item
all[j][i]
tomix
and increasej
by one to move to the next array on the next iteration.
外环不会停止,直到没有留在阵所有
,这将发生在 I
已经超过了最长阵列的长度
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"]
这篇关于通过多个阵列循环,结合到单一阵列,子阵内维持指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!