Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sum 2d array javascript

Make sure you provide the initialValue (the second positional argument) explicity as 0 for the summation to work. Otherwise the first element will be used, which is [1,0,0], which will be converted to a string 1,0,0:

A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value

See working example here:

const array = [
  [1, 0, 0],
  [1, 1, 0],
  [1, 1, 1]
]

const twoDsum = a => a.reduce((r, x) => r + x.reduce((s, y) => s + y, 0), 0);

console.log(twoDsum(array));
Comment

PREVIOUS NEXT
Code Example
Javascript :: json to array javascript 
Javascript :: mangoose connection 
Javascript :: how to do an isogram in javascript 
Javascript :: create audio tag javascript 
Javascript :: what is linter javascript 
Javascript :: what is a promise 
Javascript :: Vuejs + Laravel router redirection issue 
Javascript :: for in javascript 
Javascript :: using fb login with angular app 
Javascript :: cors axios 
Javascript :: how to get data from input field in react js 
Javascript :: yup validation based on another field value 
Javascript :: how to check if json data is received in ajax response 
Javascript :: Icons library in react 
Javascript :: new Map() collection in react state 
Javascript :: disable zoom in app 
Javascript :: js fetch status of 500 
Javascript :: array.splice javascript 
Javascript :: vue component lifecycle 
Javascript :: Uncaught (in promise) Error: Redirected when going from "/login" to "/" via a navigation guard. 
Javascript :: js html editor 
Javascript :: remove an element from array javascript 
Javascript :: overflowy javascript 
Javascript :: function for flatten an array 
Javascript :: jquery validate on keyup 
Javascript :: html form data to json 
Javascript :: findoneandupdate mongoose 
Javascript :: yarn globakl 
Javascript :: expo react navigation 
Javascript :: mongoose select 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =