最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sum of multiple array elements - Stack Overflow

programmeradmin6浏览0评论

I have multiple arrays in javascript and I want to do sum of those arrays and final array.

EX: Array1 = [1,9,10,11], Array2 = [5,8,7,2], Total = [6,17,17,13].

I have multiple arrays in javascript and I want to do sum of those arrays and final array.

EX: Array1 = [1,9,10,11], Array2 = [5,8,7,2], Total = [6,17,17,13].
Share Improve this question edited Jul 30, 2012 at 6:12 Musa 97.7k17 gold badges122 silver badges142 bronze badges asked Jul 30, 2012 at 6:09 Briskstar TechnologiesBriskstar Technologies 2,25312 gold badges40 silver badges75 bronze badges 5
  • I don't really understand, what your problem is, you want to find the best solution from a performance perspective? – axl g Commented Jul 30, 2012 at 6:12
  • What is the "final array"? Do you want 1+9+10+11 + 5+8..., or do you want [1+5+6, 9+8+17, ...] – David Hedlund Commented Jul 30, 2012 at 6:12
  • I tried creating one function.. MyArray.Sum() which is returning sum of all array elements.. but I want one final array which consists of sum of all passed multiple array elements shown in above example. – Briskstar Technologies Commented Jul 30, 2012 at 6:14
  • Array1 = [1,9,10,11], Array2 = [5,8,7,2], Result = [6,17,17,13]. Result is sum of array1 and array2 elements. – Briskstar Technologies Commented Jul 30, 2012 at 6:17
  • 1 “Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it” (stackoverflow./help/on-topic), voting to close. – Andrey Tarantsov Commented Aug 2, 2014 at 17:13
Add a ment  | 

6 Answers 6

Reset to default 6

Pure function with multiple arrays:

function sum(arrays) {
  return arrays.reduce((acc, array) => acc.map((sum, i) => sum + array[i]), new Array(arrays[0].length).fill(0));
}

const arrays = [
  [4, 6, 3, 2],
  [1, 4, 7, 9],
  [4, 6, 3, 2],
  [1, 4, 7, 9]
];

const result = sum(arrays);
console.log(result);

var Array1 = [1,9,10,11];
var Array2 = [5,8,7,2]; 
var Total = [];

for( var i = 0; i < Array1.length; i++)
{
    Total.push(Array1[i]+Array2[i]);
}

BTW, starting variable names in capital letters feels awkward.

var Array1 = [1,9,10,11];
var Array2 = [5,8,7,2];
var Total = new Array();
for(var i= 0;i<Math.min(Array1.length,Array2.length);i++){
  Total.push(Array1[i]+Array2[i]);
}
alert(Total);
function aSum(/*arrays list*/){
  var total=[],undefined;
  for(var i=0,l0=arguments.length;i<l0;i++)
    for(var j=0,arg=arguments[i],l1=arg.length;j<l1;j++)
      total[j]=(total[j]==undefined?0:total[j])+arg[j];
  return total;
}


var Array1 = [1,9,10,11], Array2 = [5,8,7,2], Array3 = [1,2,3,4,8];

console.log(aSum(Array1, Array2, Array3)); // [7, 19, 20, 17, 8]

Arrow function in ES6

var Array1 = [1,9,10,11];
var Array2 = [5,8,7,2]; 
var sum = Array1.map((val, idx) => val + Array2[idx]);

console.log(sum);

const a1 = [1, 9, 10, 11];
const a2 = [5, 8, 7, 2];

const a3 = Array.from(a1, (v, i) => v + a2[i]);

console.log(a3);

发布评论

评论列表(0)

  1. 暂无评论