Encontre Sum of 2d Array em Javascript row wise e column wise

Eu quero sumr Array como este

1 1 2 = 4

2 2 1 = 5

3 3 1 = 7

= = =

6 6 4

Eu quero imprimir como esta sum de Array Usando script java em html.

One Solution collect form web for “Encontre Sum of 2d Array em Javascript row wise e column wise”

Comece por romper o problema em pedaços mais pequenos primeiro. Eu definei uma function de sum básica que é definida usando uma function de add ainda mais básica. map sum ping do map na matriz de input lhe dairá as sums horizontais.

As sums viewticais são pouco mais complicadas, mas não muito difíceis. Eu definei uma function de transpose que gira nossa matriz. Uma vez que giramos, podemos sum as linhas da mesma maneira.

Esta solução funciona em qualquer matriz MxN

 // generic, reusable functions const add = (x,y) => x + y const sum = xs => xs.reduce(add, 0) const head = ([x,...xs]) => x const tail = ([x,...xs]) => xs const transpose = ([xs, ...xxs]) => { const aux = ([x,...xs]) => x === undefined ? transpose (xxs) : [ [x, ...xxs.map(head)], ...transpose ([xs, ...xxs.map(tail)])] return xs === undefined ? [] : aux(xs) } // sample data let numbers = [ [1,1,1], [2,2,2], [3,3,3], [4,4,4] ] // rows console.log(numbers.map(sum)) // [ 3, 6, 9, 12 ] // columns console.log(transpose(numbers).map(sum)) // [ 10, 10, 10 ] ? // generic, reusable functions const add = (x,y) => x + y const sum = xs => xs.reduce(add, 0) const head = ([x,...xs]) => x const tail = ([x,...xs]) => xs const transpose = ([xs, ...xxs]) => { const aux = ([x,...xs]) => x === undefined ? transpose (xxs) : [ [x, ...xxs.map(head)], ...transpose ([xs, ...xxs.map(tail)])] return xs === undefined ? [] : aux(xs) } // sample data let numbers = [ [1,1,1], [2,2,2], [3,3,3], [4,4,4] ] // rows console.log(numbers.map(sum)) // [ 3, 6, 9, 12 ] // columns console.log(transpose(numbers).map(sum)) // [ 10, 10, 10 ] } // generic, reusable functions const add = (x,y) => x + y const sum = xs => xs.reduce(add, 0) const head = ([x,...xs]) => x const tail = ([x,...xs]) => xs const transpose = ([xs, ...xxs]) => { const aux = ([x,...xs]) => x === undefined ? transpose (xxs) : [ [x, ...xxs.map(head)], ...transpose ([xs, ...xxs.map(tail)])] return xs === undefined ? [] : aux(xs) } // sample data let numbers = [ [1,1,1], [2,2,2], [3,3,3], [4,4,4] ] // rows console.log(numbers.map(sum)) // [ 3, 6, 9, 12 ] // columns console.log(transpose(numbers).map(sum)) // [ 10, 10, 10 ] ] // generic, reusable functions const add = (x,y) => x + y const sum = xs => xs.reduce(add, 0) const head = ([x,...xs]) => x const tail = ([x,...xs]) => xs const transpose = ([xs, ...xxs]) => { const aux = ([x,...xs]) => x === undefined ? transpose (xxs) : [ [x, ...xxs.map(head)], ...transpose ([xs, ...xxs.map(tail)])] return xs === undefined ? [] : aux(xs) } // sample data let numbers = [ [1,1,1], [2,2,2], [3,3,3], [4,4,4] ] // rows console.log(numbers.map(sum)) // [ 3, 6, 9, 12 ] // columns console.log(transpose(numbers).map(sum)) // [ 10, 10, 10 ] // linhas // generic, reusable functions const add = (x,y) => x + y const sum = xs => xs.reduce(add, 0) const head = ([x,...xs]) => x const tail = ([x,...xs]) => xs const transpose = ([xs, ...xxs]) => { const aux = ([x,...xs]) => x === undefined ? transpose (xxs) : [ [x, ...xxs.map(head)], ...transpose ([xs, ...xxs.map(tail)])] return xs === undefined ? [] : aux(xs) } // sample data let numbers = [ [1,1,1], [2,2,2], [3,3,3], [4,4,4] ] // rows console.log(numbers.map(sum)) // [ 3, 6, 9, 12 ] // columns console.log(transpose(numbers).map(sum)) // [ 10, 10, 10 ] // colunas // generic, reusable functions const add = (x,y) => x + y const sum = xs => xs.reduce(add, 0) const head = ([x,...xs]) => x const tail = ([x,...xs]) => xs const transpose = ([xs, ...xxs]) => { const aux = ([x,...xs]) => x === undefined ? transpose (xxs) : [ [x, ...xxs.map(head)], ...transpose ([xs, ...xxs.map(tail)])] return xs === undefined ? [] : aux(xs) } // sample data let numbers = [ [1,1,1], [2,2,2], [3,3,3], [4,4,4] ] // rows console.log(numbers.map(sum)) // [ 3, 6, 9, 12 ] // columns console.log(transpose(numbers).map(sum)) // [ 10, 10, 10 ] 
JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.