Skip to content
Advertisement

Merge multiple outputs from multiple functions into one array in Javascript?

Example

I have these values, there is any way to put them together in a single array?

This is one of the functions that make the results, they take their data from a database.

function AddvotoTec(votor) {
  class Avg {
    constructor() {}
    static average(votor) {
      var total = 0;
      var count = 0;
      jQuery.each(votor, function(index, value) {
            total += value;
            count++;
      });
      return total / count;
    }
  }
  var mvoti = Avg.average(votor);
  voti(mvoti);
}

function voti(Voti) {
  var voti = [];
  voti.push(Voti);
  console.log(voti);
}

Advertisement

Answer

Your problem is you put var voti = [] in the function that will initialize a new array every time you call it. If you want to push data to that array, you should move it out from voti(Voti) function

function AddvotoTec(votor) {
  class Avg {
    constructor() {}
    static average(votor) {
      var total = 0;
      var count = 0;
      jQuery.each(votor, function(index, value) {
            total += value;
            count++;
      });
      return total / count;
    }
  }
  var mvoti = Avg.average(votor);
  voti(mvoti);
}

var voti = []; //move the array to the outer scope
function voti(Voti) {
  voti.push(Voti);
  console.log(voti);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement