'Merge multiple outputs from multiple functions into one array in Javascript?
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);
}
Solution 1:[1]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nick Vu |
