dc65k,
<pre>
<script>
const array = [{
'2021-07-01': {
'products': [1661.28, 229],
},
},
{
'2021-07-02': {
'fare': [66, 78],
'products': 218,
}
},
{
'2021-07-03': {
'fare': 78,
'products': 909.80,
}
},
{
'2021-07-05': {
'water': 840,
'products': 1021.61,
},
},
]
const calculate = array => Object.entries(array.reduce((obj, a) => {
let [key, value] = Object.entries(a)[0], sum = 0;
for (let k in value) {
k in obj || (obj[k] = 0);
let temp = value[k];
if(Array.isArray(temp)) temp = temp.reduce((a, b) => a + b, 0);
obj[k] += temp;
sum += temp
}
obj[key] = sum;
obj.total += sum;
return obj
}, { total: 0 })).map(([a, b]) => ({[a]: b.toFixed(2)}))
document.write(JSON.stringify(calculate(array), "", 1));
</script></pre>