Sum of elements in a multidimensional array in javascript [closed]

2

I have this arrangement and I need to add all the values of maracadores [i] [1] on the one hand and all of the [i] [2] on the other . And what I propose only adds the element where the for is 2 times, then at the end x only adds me 16.75 + 16.75

var marcadores = [
      ['x',19.04, -98.20],
      ['x', 25.54, -103.40],
      ['x', 20.65, -103.349],
      ['x',16.75,-93.129]

    ];
    
    for (var i = 0; i < marcadores.length; i++) {
      x = marcadores[i][1]+ marcadores[i][1];
      $('#placeholder').html(x);
      // o algo así?
    }
    
asked by E.Rawrdríguez.Ophanim 06.03.2018 в 01:50
source

5 answers

3

The way to add all the values of the array would be the following, accumulating all the values in an adder

Example of code that works.

 var marcadores = [
      ['x',19.04, -98.20],
      ['x', 25.54, -103.40],
      ['x', 20.65, -103.349],
      ['x',16.75,-93.129]

    ];
    var sumador=0;
    for (var i = 0; i < marcadores.length; i++) {
      sumador = sumador+marcadores[i][1]+ marcadores[i][2];

    }

Apart from that in your code you are adding twice the same x = markers [i] [1] + markers [i] [1];

The way to add both columns separately involves two adders

 var marcadores = [
      ['x',19.04, -98.20],
      ['x', 25.54, -103.40],
      ['x', 20.65, -103.349],
      ['x',16.75,-93.129]

    ];
    var sumadorA=0;
    var sumadorB=0;
    for (var i = 0; i < marcadores.length; i++) {
      sumadorA = sumadorA+marcadores[i][1]
      sumadorB= sumadorB+marcadores[i][2];    
    }


 </script>

Greetings.

    
answered by 06.03.2018 / 01:53
source
1

You are adding twice the same value and also to put x = you are only assigning the last value. You should add it to all the values you pass by setting x += .

And to add it separately you need two variables:

var marcadores = [
    ['x',19.04, -98.20],
    ['x', 25.54, -103.40],
    ['x', 20.65, -103.349],
    ['x',16.75,-93.129]

];


var valor1 = 0;
var valor2 = 0;
for (var i = 0; i < marcadores.length; i++) {
   valor1  += marcadores[i][1];
   valor2 += marcadores[i][2];
}
    
answered by 06.03.2018 в 02:12
1

To add the columns you have to obtain how many rows there are, so 'marker [0] .length'

var valor1 = 0;
var valor2 = 0;
for (var i = 0; i < marcadores[0].length; i++) {
   valor1  += marcadores[i][1];
   valor2 += marcadores[i][2];
}
    
answered by 06.03.2018 в 07:32
1

Maybe with reduce you can do it very easy.

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
  

The method reduces () applies a function to an accumulator and to each value of an array (from left to right) to reduce it to a single value.

Code:

var marcadores = [
  ['x', 19.04, -98.20],
  ['x', 25.54, -103.40],
  ['x', 20.65, -103.349],
  ['x', 16.75, -93.129]
];

var sumaA = marcadores.reduce(function(sum, col) {
  return sum + col[1];
}, 0);
var sumaB = marcadores.reduce(function(sum, col) {
  return sum + col[2];
}, 0);

document.getElementById("sumaA").innerHTML = sumaA;
document.getElementById("sumaB").innerHTML = sumaB;
<label id="sumaA"></label>
<label id="sumaB"></label>
answered by 06.03.2018 в 09:52
-1

The for would be something like this:

var sumaX;
for (var i = 0; i < marcadores.length; i++) {
      x = marcadores[i][1]+ marcadores[i][2];
      sumaX += X;
}

$('#placeholder').html(sumaX);
    
answered by 06.03.2018 в 01:54