-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

Bradley Joe
  • 63
  • 1
  • 6
  • 4
    Welcome to StackOverflow! Remember to include what you've tried in your question. – zfrisch Aug 07 '18 at 15:47
  • 1
    Welcome to Stackoverflow, to get the best help read this guide before posting a question: https://stackoverflow.com/help/how-to-ask – amyloula Aug 07 '18 at 15:47

5 Answers5

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);
zfrisch
  • 8,474
  • 1
  • 22
  • 34
2
let array3 = [];
for(let i = 0; i < array1.length; i++){
  array3.push(array1[i]);
  array3.push(array2[i]);
}
Adam
  • 1,724
  • 13
  • 16
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);
Mamun
  • 66,969
  • 9
  • 47
  • 59
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);
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
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]);
}
Bhavin
  • 2,070
  • 6
  • 35
  • 54