Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two arrays in JavaScript

Can I merge two arrays in JavaScript like this?

these arrays:

arr1 = ['one','two','three'];
arr2 = [1,2,3];

into

arr3 = ['one': 1, 'two': 2, 'three' : 3]
like image 665
Sam Avatar asked Jul 14 '12 20:07

Sam


People also ask

Can you combine two arrays in JavaScript?

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

How do I combine two arrays?

In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

How do I put multiple arrays into one array?

Using concat method The concat method will merge two or more arrays into a single array and return a new array.

How do I combine two arrays in TypeScript?

TypeScript - Array concat() concat() method returns a new array comprised of this array joined with two or more arrays.


1 Answers

var arr3 = {};
for (var i = 0; i < arr1.length; i++) {
    arr3[arr1[i]] = arr2[i];
}

Please note that arr3 is not array, it is object.

like image 55
VisioN Avatar answered Sep 21 '22 07:09

VisioN