Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sum of two array in javascript

function sumArray(a, b) {
      var c = [];
      for (var i = 0; i < Math.max(a.length, b.length); i++) {
        c.push((a[i] || 0) + (b[i] || 0));
      }
      return c;
}
Comment

sum of two array in javascript

function sumArrays(...arrays) {
  const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
  const result = Array.from({ length: n });
  return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
}

console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
Comment

javascript sum of multiple array

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);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: add navbar active 
Javascript :: add svg in react 
Javascript :: add object in array state react 
Javascript :: react useEffect life cycle 
Javascript :: Iterate Through the Keys of an Object 
Javascript :: javascript this 
Javascript :: axios js 
Javascript :: Get last item on js array 
Javascript :: make table responsive react-bootstrap-table2 
Javascript :: discord.js create channel and get id 
Javascript :: what is getter and setter in javascript 
Javascript :: react-native-bouncy-checkbox 
Javascript :: v-on shorthand 
Javascript :: chain id 
Javascript :: sequelize manual model/index.js 
Javascript :: filter object array 
Javascript :: csurf in express 
Javascript :: sequelize association alias 
Javascript :: find multiples of a number 
Javascript :: modify array elements javascript 
Javascript :: onomonrieah 
Javascript :: Return a Sorted Array Without Changing the Original Array 
Javascript :: javascript Create a RegEx 
Javascript :: console.log printing object object 
Javascript :: how to make if method inside an if methen in fandom 
Javascript :: apollo uselazyquery oncompleted 
Javascript :: nodejs get appdata path 
Javascript :: notification like whatsapp in jquery 
Javascript :: mongodb js insertmany 
Javascript :: recoil js 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =