1

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:

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

1
  • Is it always 4 arrays? Are the lengths always the same? These are the kinds of questions that need to be answered before finding a solution. Commented Dec 2, 2017 at 3:43

4 Answers 4

2

Assuming each source array is the same length:

a.forEach((e, i) => {
  neat.push(e, b[i], c[i], d[i]);
};
Sign up to request clarification or add additional context in comments.

1 Comment

they are the same length for my case
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);

4 Comments

Do you believe that your solution is better than Justin's ?
With respect to the Time complexity, both of them run in O(n).
@Taurus "better" is very subjective. for loop is generally the fastest, and works in all browsers (arrow functions are not supported in Internet Explorer)
@Slai Let's replace "better" with "more efficient". But, it seems cleaner to me to do it using a built-in function (rather than a for loop).
1

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

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.

See JSFiddle

4 Comments

You should actually use a.length >= i, because a[i] != undefined will return false for both null and undefined indices. Also, decrementing 1 from maxItems in the loop condition is unnecessary and will omit the last item of the largest array.
@Taurus Well, you don't want null and undefined values most likely given the data sets are integers and strings. Indexes in arrays start with 0, taking 1 away is needed or else you'll end up doing the last iteration on out of index values a.k.a undefined values as Math.max will return the number of items within the array. Say there are 6 items, maxItems = 6, the max index within an array is 5, so last iteration is redundant.
Yes, but you are saying i < maxItems - 1, so, you are actually saying i < 5, so the index 5 will not be iterated through.
You're right, I just noticed what I've done, newbie mistake. Apologies and thanks for pointing it out :)
-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);

2 Comments

You know you can add a series of items using just one .push? See other answers below which are the same as yours and show this functionality.
Indeed, your answer is the same as the other answers here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.