Combine two arrays in JavaScript

后端 未结 5 1975
無奈伤痛
無奈伤痛 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条回答
  •  猫巷女王i
    2020-12-17 00:14

    Just because you said in jQuery, here's a jQuery$.each version.

    arr1 = ['one','two','three'];
    arr2 = [1,2,3];
    arr3 = {};
    $.each(arr1, function(i, value){
      arr3[value] = arr2[i];
    });
    console.log(JSON.stringify(arr3));
    

    output ->

    {"one":1,"two":2,"three":3}
    

    here's a working jsFiddle

提交回复
热议问题