Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140447

How can i merge two arrays in one array of arrays in Javascript? [duplicate]

$
0
0

I have the following two arrays:

const firstArr = [1, 2, 3, 4]
const secondArr = ['somevalue', 'anothervalue', 'another', 'other']

I would like to get the following output:

const final = [[1, 'somevalue'], [2, 'anothervalue'], [3, 'another'], [4, 'other']]

Where each value of the two arrays is associated with another value and put into an array. So for example, the first value of firstArr should be associated with the first value of secondArr in a sub-array on the final array.

Is there some function in Javascript to do this or am i going to have to do it another way, for example with a loop?


Viewing all articles
Browse latest Browse all 140447

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>