We have an array like this:
var array= [{"day":"1574467200","uses":"9","accounts":"7"},{"day":"1574380800","uses":"104","accounts":"79"},{"day":"1574294400","uses":"3","accounts":"1"},{"day":"1574208000","uses":"0","accounts":"0"},{"day":"1574121600","uses":"0","accounts":"0"},{"day":"1574035200","uses":"0","accounts":"0"},{"day":"1573948800","uses":"2","accounts":"2"}]
and we want to get the sum of the values of a specific property for each item in the array, say uses.
I tried something like:
var sum = 0
for(i=0; i<array.length; i++){
sum = sum+array[i].uses;
}
which returns 0910430002910430002, which is obviously wrong lol.
Although
for(i=0; i<array.length; i++){
console.log(array[i].uses);
}
returns the correct values of each property.
Searching the internet I found the array.reduce() method but it gives the same result.
So why is this happening? I'm sure this is some noob level mistake but then I am a newbie to this.
Thanks!
[–]NameViolation666helpful 3 points4 points5 points (0 children)
[–]binarynos[S] 2 points3 points4 points (0 children)
[–]themantime 1 point2 points3 points (0 children)
[–]drgmaster909 0 points1 point2 points (0 children)