Merge multiple arrays based on their index using javascript

前端 未结 7 919
清歌不尽
清歌不尽 2020-12-11 16:44

I need to merge two arrays into a single array. I have code but it is not working as expected-- it is merging them one after another, but I need to interlock the values.

7条回答
  •  天命终不由人
    2020-12-11 17:22

    var array1 = [1, 2, 3, 4];
    var array2 = ["a", "b", "c", "d"];
    var array3 = ["f", "d", "s", "a"];
    
    var newArray = array1.map(function(value, index) {
      return value + array2[index] + array3[index];
    });
    console.log(newArray);
    

提交回复
热议问题