Javascript - Sum two arrays in single iteration

后端 未结 13 2092
我寻月下人不归
我寻月下人不归 2020-11-29 05:56

I want to sum each value of an array of numbers with its corresponding value in a different array of numbers, and I want to do this without looping through each individual v

13条回答
  •  被撕碎了的回忆
    2020-11-29 06:15

    Another way to do it could be like that

    var array1 = [1,2,3,4];
    var array2 = [5,6,7,8];
    
    var sum = [...array1].map((e,i)=> e+array2[i]); //[6,8,10,12]
    

    In this case [...array1] is the same to [1,2,3,4]

提交回复
热议问题