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]
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.
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.
Using concat method The concat method will merge two or more arrays into a single array and return a new array.
TypeScript - Array concat() concat() method returns a new array comprised of this array joined with two or more arrays.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With