javascript 循环遍历多个数组,组合成单个数组,维护子数组内的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19009868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through multiple arrays, combining to single array,maintaining indexes within the subarray
提问by TommyBs
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 hshould not be before iin the bottom example.
有点冗长的标题,但我会尽力解释我想做什么。基本上我有许多数组,我想将它们组合成一个数组。问题是,我需要遍历子数组中的项目并一次添加 1 并保持顺序。最终目标是我想显示分页的数据。我在下面有一个简单的例子,我将用它来尝试传达我的意思。这不是按字母顺序排序,即在底部示例中h不应在i之前。
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.
所以在我的例子中,我知道我想要 3 页的结果。第一个页面应该有 4 个项目,第二个页面应该有 4 个项目,第三个只有 1 个项目。
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.
我预先获得了页面变量,但我不确定如何构建循环。我想我基本上需要遍历每个子数组并 pop() 关闭第一项,然后遍历下一个, pop() 第一项等等。但是在某处我需要检查每个子数组中还剩下多少项。
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.
例如,如果我只有数组“一个”,理论上我会有 2 页,第一页包含 a、c、e、i,第二页只有 k,这显然很简单,因为我只检查唯一数组的长度。
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.
但是如果我添加另一个数组“第三个”[1,2,3,4,5] 那么我希望混合数组是 ['a','b',1,'c','d',2 ...等等]; 这些数组中的每一个理论上都可以有不同的长度,所以我显然会跳过一个空值。
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.
如有任何问题或不清楚的地方,请发表评论。
采纳答案by Andrew Clark
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:
在外循环的每次迭代中,我们增加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
超出数组的最大索引,all[j]
我们就完成了该数组的处理,因此使用all.splice(j, 1)
. 我们不前进,j
因为all[j]
将在删除该位置的前一个元素后引用下一个数组。 - 否则,我们将项目添加
all[j][i]
到mix
并增加j
一个以在下一次迭代中移动到下一个数组。
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.
外循环直到 中没有数组时才会停止all
,当i
超过最长数组的长度时会发生这种情况。
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"]
回答by Guffa
Loop through the inner index in the outer loop, and the arrays in the inner loop:
循环遍历外循环中的内索引和内循环中的数组:
for (var i = 0, cont = true; cont; i++) {
cont = false;
for (j = 0; j < all.length; j++) {
if (i < all[j].length) {
mix.push(all[j][i]);
cont = true;
}
}
}
回答by jonhopkins
It looks like you want a simple merge function. You could do it like this
看起来您想要一个简单的合并功能。你可以这样做
var one = ['a','c','e','i','k'];
var two = ['b','d','f','h'];
var mix = new Array();
var merging = true;
var index = 0;
while (merging) {
merging = false;
if (index < one.length) {
mix[mix.length] = one[index];
merging = true;
}
if (index < two.length) {
mix[mix.length] = two[index];
merging = true;
}
// add if blocks for arrays three, four, etc...
index++;
}
This can be expanded for any number of arrays by simply adding another if block inside the loop
这可以通过简单地在循环内添加另一个 if 块来扩展任意数量的数组