Combine the values of two arrays into object

前端 未结 5 1547
遥遥无期
遥遥无期 2021-01-21 08:43

I have two arrays:

array1 = [\"Bob\", \"John\", \"Dave\"];
array2 = [1, 2, 3];

Is there combine the two into a javascript array filled with obj

5条回答
  •  执笔经年
    2021-01-21 09:13

    Simple solution using Array.prototype.map() function:

    var array1 = ["Bob", "John", "Dave"], 
        array2 = [1, 2, 3],
        combined = array1.map(function(v, k, a){ return {meta: v, value: array2[k]}; });
        
    console.log(combined);

提交回复
热议问题