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

Looping through two arrays to fill a two dimensionnal array

I work on Angular and use Typescript. I have two arrays array1 and array2 which I have parsed from an API.

A console.log on array1 looks like this :
enter image description here

A console.log on array2 looks like this :
enter image description here

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

I’d like to create an two dimensionnal array which would merge the two arrays one element by element (id 0 with id 0, id 1 with id 1, id 2 with id 2 and so on). To be clearer the result would be :

[["Outils sali", "saunier"], ["outils elem", "outils trad"], ["outils trad", "outils sali"], .... ];

Would you have any ideas to realize that trick ?

Any help would be very appreciated, thank you !

>Solution :

If both have the same length just use a map operator

array1 = []; // imagine filled
array2  = []; // imagine filled

let result  = array1.map((array1Value, index) => [array1Value, array2[index]]); 
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