1

I have 2 arrays :

const firstArr = [1,2,3,4,5,6]; 

const secondArr = [7,8,9,10,11,12];

I am trying to use a map loop like this :

arr.map(num1 => arr1.map(num2 => `${[num1]} ${num2}`))

I need the results of the second first array inside the results of the second one. Right now with the method i did it, Everything is duplicating:

0: (6) ["1,7", "1,8", "1,9", "1,10", "1,11", "1,12"]
1: (6) ["2,7", "2,8", "2,9", "2,10", "2,11", "2,12"]
2: (6) ["3,7", "3,8", "3,9", "3,10", "3,11", "3,12"]
3: (6) ["4,7", "4,8", "4,9", "4,10", "4,11", "4,12"]
4: (6) ["5,7", "5,8", "5,9", "5,10", "5,11", "5,12"]
5: (6) ["6,7", "6,8", "6,9", "6,10", "6,11", "6,12"]

Is it possible to have the results only once ? My expected result is ['1,7' , '2,8' , " 3,9"] and so on.

Sorry I am still a noob :/ ...

Why u do dis
  • 418
  • 4
  • 15
  • It seems to me you are getting the results only once. "I need the results of the second first array inside the results of the second one. " This can be interpreted different ways. Your question is unclear. –  Mar 11 '21 at 19:40
  • @SebastianSimon My expected result is ['1,7' , '2,8' , " 3,9"] and so on... Thank you for your comment – Why u do dis Mar 11 '21 at 19:41
  • 4
    Does this help you? https://stackoverflow.com/questions/22015684/how-do-i-zip-two-arrays-in-javascript –  Mar 11 '21 at 19:41

1 Answers1

1

You can use the index in .map as follows:

const firstArr =  [1,2,3,4,5,6]; 
const secondArr = [7,8,9,10,11,12];

const res = firstArr.map((num1, i) =>`${[num1]},${secondArr[i]}`);

console.log(res);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48