Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concat array elements into another array by index order

I’ve been trying to get a specific result to this issue but couldn’t find an answer that suits it, so I’m working with 2 arrays of the same length:

arr1 = [value1,value2,value3]

arr2=[otherValue1,otherValue2,otherValue3]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

how can I concat the 2 arrays by the order of the index?

expected result = [value1,otherValue1,value2,otherValue2,value3,otherValue3]

Sorry if this question is a duplicate, but couldn’t find my expected result by using concat or splice.

>Solution :

let arr1 = ['value1','value2','value3'];

let arr2=['otherValue1','otherValue2','otherValue3'];

let result = [];

for(let i =0; i<arr1.length; i++){
  result.push(arr1[i]);
  result.push(arr2[i]);
}

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading