0
Good morning to you all. I have the following code:
var somaR = 0;
var somaN = 0;
var project = [
{part: 'Renault', partNumber: 1234, project: 'X19', gap: 15.000},
{part: 'Renault', partNumber: 1234, project: 'X19', gap: 600},
]
function groupBy(array, f) {
var groups = {};
array.forEach(function(o) {
var group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function(group) {
return groups[group];
});
}
var projectIguais = groupBy(project, function(item) {
return [item.part, item.partNumber, item.project];
}).map(function(data){
return {
totalGap: data.reduce(function(total, valor){return total + valor.gap;}, 0),
itens: data
};
});
console.log(projectIguais);
for (var i in projectIguais) {
var partNumberItem = "Part Number: " +projectIguais[i].itens[0].partNumber;
var projectItem = "Project: " +projectIguais[i].itens[0].project;
var gap = "Gap: "+projectIguais[i].itens[0].gap;
var totalGapItem = projectIguais[i].totalGap;
console.log(partNumberItem);
console.log(projectItem);
console.log(gap);
if(projectIguais[i].itens[0].part == 'Renault'){
somaR += totalGapItem;
}
if(projectIguais[i].itens[0].part == 'Nissan'){
somaN += totalGapItem;
}
}
console.log("Soma Renault", somaR);
console.log("Soma Nissan", somaN);
Inside the project array I have the gap 15,000 but when the calculation is done, the gap value is 15.
Exp: The code takes inside the array the same partNumber and project, sums the gap and returns the total of the gap per project. In my array I have a gap 15,000 and another gap 600 but in the calculation it is returning me gap 615 because it is removing the zeroes after the point.
How do I keep the 15,000 in the calculation?
Json is wrong, the correct one for numerical json in thousands is to receive without any point, Exp:
15000
, the.
separates the fraction, Exp:15000.58 => 15.000,58
– MarceloBoni