-3

Basically I need to add each item from one array after each on from another array. So if these are the two arrays:

array1 = [
"item1",
"item2",
"item3",
"item4"
];

array2 = [
"choice1",
"choice2",
"choice3",
"choice4"
];

I need to make array1 become this:

"item1",
"choice1",
"item2",
"choice2",
"item3",
"choice3",
"item4",
"choice4"
];

Does anyone have any idea how to do this? Thanks

2
  • 4
    Welcome to StackOverflow! Remember to include what you've tried in your question. Commented Aug 7, 2018 at 15:47
  • 1
    Welcome to Stackoverflow, to get the best help read this guide before posting a question: stackoverflow.com/help/how-to-ask Commented Aug 7, 2018 at 15:47

5 Answers 5

3

Given that the arrays are the same length you can map over one of them, provide a return value of an array with both values at the index from both arrays, and then flatten with concat for your wanted result.

[].concat.apply([], array1.map((i, ind) => [i,array2[ind]]));

let a1 = ["item1","item2","item3","item4"], a2 = ["choice1","choice2","choice3","choice4"],
combined_array = [].concat.apply([], a1.map((i, ind) => [i,a2[ind]]));

console.log(combined_array);

OR

You could similarly use reduce. This may be a better option if you would like to keep away from calling concat off an Array object:

array1.reduce((acc, i, ind) => acc.push(i, array2[ind])&&acc, []);

    let a1 = ["item1","item2","item3","item4"], a2 = ["choice1","choice2","choice3","choice4"],
    combined_array = a1.reduce((acc, i, ind) => acc.push(i, a2[ind])&&acc, []);
   

    console.log(combined_array);

Sign up to request clarification or add additional context in comments.

Comments

2
let array3 = [];
for(let i = 0; i < array1.length; i++){
  array3.push(array1[i]);
  array3.push(array2[i]);
}

Comments

2

You can use forEach() to iterate over the first array. Then push the current item to the result array and use index to take the item from the second array to push as the next item:

var array1 = [
"item1",
"item2",
"item3",
"item4"
];

var array2 = [
"choice1",
"choice2",
"choice3",
"choice4"
];

var res = [];
array1.forEach((i,idx) =>{
  res.push(i);
  res.push(array2[idx]);
});
console.log(res);

Comments

1

You could loop through them and add them to an array using the index to the element of every level after each others :

array1 = [
  "item1",
  "item2",
  "item3",
  "item4"
];

array2 = [
  "choice1",
  "choice2",
  "choice3",
  "choice4"
];

var result = [];

for (var i = 0; i < array1.length; i++) {
  result.push(array1[i]);
  result.push(array2[i]);
}

console.log(result);

Comments

0

You can use push() method here,

array1 = [
"item1",
"item2",
"item3",
"item4"
];

array2 = [
"choice1",
"choice2",
"choice3",
"choice4"
];

var FinalArray = [];
for(var index = 0; index < array1.length; index++){
  FinalArray.push(array1[index]);
  FinalArray.push(array2[index]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.