从每个javascript数组concat获取1个元素然后循环到一个数组

时间:2021-01-13 03:35:10

I have 4 arrays, I'd like to combine them into 1. I can do that, but I'd like to take one element from each array, push it to my new array, then get the next 4 and so on. This is what I got:

我有4个数组,我想将它们组合成1.我可以这样做,但我想从每个数组中取一个元素,将它推送到我的新数组,然后获得接下来的4个,依此类推。这就是我得到的:

var a = [ "foo", "bar", "baz", "bam", "bun", "fun" ];
var b = [ 1, 2, 3, 4, 5, 6];
var c=["a","b","c","d","e","f"];
var d=[7,8,9,10,11,12]
var neat=[];
neat= a.concat(b, c,d);
//neat=["foo","bar","baz","bam","bun","fun",1,2,3,4,5,6,"a","b","c","d","e","f",7,8,9,10,11, 12]

The result I want would be something like this:

我想要的结果是这样的:

//neat=["foo",1,"a",7,"bar",2,"b",8...]

I'm not sure if a loop will work or if I need to use another function

我不确定循环是否有效或是否需要使用其他功能

4 个解决方案

#1


1  

Please try the below code :

请尝试以下代码:

var a = [ "foo", "bar", "baz", "bam", "bun", "fun" ];
var b = [ 1, 2, 3, 4, 5, 6];
var c=["a","b","c","d","e","f"];
var d=[7,8,9,10,11,12]
var neat=[];
//neat= a.concat(b, c,d);
//neat=["foo","bar","baz","b



for (var i = 0; i < a.length ; i++) 
{
neat.push(a[i], b[i], c[i], d[i]);
}
console.log(neat);

#2


2  

Assuming each source array is the same length:

假设每个源数组的长度相同:

a.forEach((e, i) => {
  neat.push(e, b[i], c[i], d[i]);
};

#3


1  

While Justins answer is correct, however if the lengths of the array are not the same every time, you could do

虽然Justins的回答是正确的,但是如果阵列的长度每次都不相同,那么你可以做到

var maxItems = Math.max(a.length,b.length,c.length,d.length);

var neat = [];

for(var i = 0; i < maxItems; i++){

  if(a[i] != undefined){
    neat.push(a[i]);
  }

  if(b[i] != undefined){
    neat.push(b[i]);
  }

  if(c[i] != undefined){
    neat.push(c[i]);
  }

  if(d[i] != undefined){
    neat.push(d[i]);
  }
}

Math.max would find the biggest number of entries from between the 4 arrays, then a simple for loop on that number and check if the value is undefinedbefore pushing it to neat array.

Math.max会从4个数组中找到最大数量的条目,然后在该数字上进行简单的for循环,并在将其推送到整齐数组之前检查该值是否未定义。

See JSFiddle

#4


-1  

Because the length of the all arrays are equal. So we can easily do that using loop.

因为所有数组的长度相等。所以我们可以使用循环轻松做到这一点。

var a = [ "foo", "bar", "baz", "bam", "bun", "fun" ];
var b = [ 1, 2, 3, 4, 5, 6];
var c=["a","b","c","d","e","f"];
var d=[7,8,9,10,11,12]
var neat=[], i;

for(i=0;i<a.length;i++){
    neat.push(a[i]);
    neat.push(b[i]);
    neat.push(c[i]);
    neat.push(d[i]);
}
console.log(neat);

#1


1  

Please try the below code :

请尝试以下代码:

var a = [ "foo", "bar", "baz", "bam", "bun", "fun" ];
var b = [ 1, 2, 3, 4, 5, 6];
var c=["a","b","c","d","e","f"];
var d=[7,8,9,10,11,12]
var neat=[];
//neat= a.concat(b, c,d);
//neat=["foo","bar","baz","b



for (var i = 0; i < a.length ; i++) 
{
neat.push(a[i], b[i], c[i], d[i]);
}
console.log(neat);

#2


2  

Assuming each source array is the same length:

假设每个源数组的长度相同:

a.forEach((e, i) => {
  neat.push(e, b[i], c[i], d[i]);
};

#3


1  

While Justins answer is correct, however if the lengths of the array are not the same every time, you could do

虽然Justins的回答是正确的,但是如果阵列的长度每次都不相同,那么你可以做到

var maxItems = Math.max(a.length,b.length,c.length,d.length);

var neat = [];

for(var i = 0; i < maxItems; i++){

  if(a[i] != undefined){
    neat.push(a[i]);
  }

  if(b[i] != undefined){
    neat.push(b[i]);
  }

  if(c[i] != undefined){
    neat.push(c[i]);
  }

  if(d[i] != undefined){
    neat.push(d[i]);
  }
}

Math.max would find the biggest number of entries from between the 4 arrays, then a simple for loop on that number and check if the value is undefinedbefore pushing it to neat array.

Math.max会从4个数组中找到最大数量的条目,然后在该数字上进行简单的for循环,并在将其推送到整齐数组之前检查该值是否未定义。

See JSFiddle

#4


-1  

Because the length of the all arrays are equal. So we can easily do that using loop.

因为所有数组的长度相等。所以我们可以使用循环轻松做到这一点。

var a = [ "foo", "bar", "baz", "bam", "bun", "fun" ];
var b = [ 1, 2, 3, 4, 5, 6];
var c=["a","b","c","d","e","f"];
var d=[7,8,9,10,11,12]
var neat=[], i;

for(i=0;i<a.length;i++){
    neat.push(a[i]);
    neat.push(b[i]);
    neat.push(c[i]);
    neat.push(d[i]);
}
console.log(neat);