Javascript - Sum two arrays in single iteration

后端 未结 13 1985
我寻月下人不归
我寻月下人不归 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:13

    You can use the _.unzipWith method from the Lodash library.

    var array1 = [1, 2, 3, 4];
    var array2 = [5, 6, 7, 8];
    var array = [array1, array2];
    console.log(_.unzipWith(array, _.add));

提交回复
热议问题