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 => {
let obj = {}, total = 0
array = array.map(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
}
total += sum
return {
[key]: sum.toFixed(2)
}
});
total = total.toFixed(2);
array.push({ total });
for (let k in obj) obj[k] = obj[k].toFixed(2);
array.push(obj);
return array
}
document.write(JSON.stringify(calculate(array), "", 1));
</script></pre>