Combine two arrays in JavaScript

后端 未结 5 1947
無奈伤痛
無奈伤痛 2020-12-16 23:53

Can I merge two arrays in JavaScript like this?

these arrays:

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

into

<         


        
5条回答
  •  春和景丽
    2020-12-17 00:10

    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.

提交回复
热议问题