2
I’m making an AJAX call and it’s returning me an array of objects as follows:
[
{
"area": "607,55"
},
{
"area": "2.415,67"
},
{
"area": "280,53"
},
{
"area": "203,05"
},
{
"area": "296,13"
}
]
I would like to sum up all the areas, and I am doing it as follows using the .replace
to change the comma:
let calculaArea = [];
result.forEach(function (result) {
calculaArea.push(parseFloat(result.area.replace(/,/,'')));
});
//console.log(calculaArea)
[
60755,
2.41567,
28053,
20305,
29613
]
/////retornando array com numeros sem virgula/////
Next I’m adding the array of objects like this:
let totalArea = calculaArea.reduce(function (a, b) {
return (a + b);
});
///console.log(totalArea);
///
138728.41567000002
///
I’ve looked at several answers here but they only work with a few houses - example: 3.28 = 3,28
.
Is there a more effective method for adding an array that contains semicolons, and holds 2 squares after the comma?
friend @hkotsubo thank you so much also tested your code by changing some things and it worked perfectly, I wanted to know if it is possible with this result to add the "," comma to get real float in this example "3.802,93" thanks even!!
– luiz_vitorr
@luiz_vitorr I updated the answer with this option. And just to remind you how the site works: if one of the answers solved your problem, you can choose the one that best solved it and accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. Don’t forget that you can also vote in all the answers you found useful.
– hkotsubo
Thank you @hkotsubo I will be positivizing the reply and say that you were of great help by the tips
– luiz_vitorr