In my Chrome console, I have a table called data which has 23 rows and look like this: data table in Chrome console
Then I select the first row with data[0]
, it looks like this:
data[0]
My objective is to sum all the values of this row (and each row), how to do this?
Thank you,
This will sum the values in each row of data
, except the hour
value, which is a date.
var sums = Array.prototype.slice.call(data, 0).map(function(item) {
return Object.keys(item)
.filter(function (key) { return key !== 'hour'})
.reduce(function(total, key) {
return total + item[key]
}, 0)
})
data
is an array-like object, so we call slice
on it first to return an array. Next we map each item in the array. This mapping is a sum of all the values of each element in the array, except for hour
, which is a date, so it gets filtered out of the keys list. The final value of sums
is an array of totals from each object in data
.