How to subtract one array from another, element-wise, in javascript

前端 未结 4 1232
执念已碎
执念已碎 2021-02-12 05:25

If i have an array A = [1, 4, 3, 2] and B = [0, 2, 1, 2] I want to return a new array (A - B) with values [1, 2, 2, 0]. What is the most e

4条回答
  •  礼貌的吻别
    2021-02-12 06:06

    For Simple and efficient ever.

    Check here : JsPref - For Vs Map Vs forEach

    var a = [1, 4, 3, 2],
      b = [0, 2, 1, 2],
      x = [];
    
    for(var i = 0;i<=b.length-1;i++)
      x.push(a[i] - b[i]);
      
    console.log(x);

提交回复
热议问题