I have 2 arrays with different length and the number of items in each array is dynamic. I want to combine the first element of one array with first element of second array and first element of first array with second element of second array and so on.for example:
The first array is ['a','b','c']
The second array is ['1','2]
Finally, I need the result to look like this:
['a-1','a-2','b-1','b-2','c-1','c-2]
2
Answers
You can try use
flatMap()
to iterate over each element ofarray1
and usemap()
on array2:You can achieve the result using for loop as well:
To combine the elements of the first array with the elements of the second array as described, you can use array methods like
map
andflatMap
in JavaScript.