I have 3 objects in an array in which all of them have an equal number of data properties. The data is as follows:
const monthStats = [{
name: 'pending',
data: ['5', '1', '2', '3', '100', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '20', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}, {
name: 'delivered',
data: ['10', '44', '12', '0', '250', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '180', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}, {
name: 'failed',
data: ['15', '33', '30', '2', '150', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '50', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}];
The data is representation of date values in a month(So they have equal length). What I want is to get the sum of values of alternate data of each dates and get the maximum value out of it.
Alternate here means for example: on pending 0th index has value 5, similarly on delivered is 10 and on failed is 15. Their total sum is 30
. Here, the maximum value in the above example is 500
on the 4th index after summing them and that's what I want.
How can I achieve this?
Here is what you want as per my understanding:
const monthStats = [{
name: 'pending',
data: ['5', '1', '2', '3', '100', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '20', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}, {
name: 'delivered',
data: ['10', '44', '12', '0', '250', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '180', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}, {
name: 'failed',
data: ['15', '33', '30', '150', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '50', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}];
let index=0;
let max=0;
for(let i=0;i<monthStats[0].data.length;i++){
let sum=+monthStats[0].data[i]+ +monthStats[1].data[i]+ +monthStats[2].data[i]
if(max<sum){
max=sum
index=i
}
}
console.log(`Max sum is ${max} at index ${index}`)
Assuming that the pending, delivered and failed objects stay in that order (which is not a good practice, but.. well).
You can simply add the elements of each value of the data arrays, then get the max value of that sum, and then get the index of that max value.
const monthStats = [{
name: 'pending',
data: ['5', '1', '2', '3', '100', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '20', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}, {
name: 'delivered',
data: ['10', '44', '12', '0', '250', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '180', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}, {
name: 'failed',
data: ['15', '33', '30', '150', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '50', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
}];
const monthTotals = monthStats[0].data.map((pend, pIndex) => Number(pend) + Number(monthStats[1].data[pIndex]) + Number(monthStats[2].data[pIndex]));
const maxMonthTotal = Math.max(...monthTotals);
const maxMonthTotalIndex = monthTotals.findIndex(monthTotal => maxMonthTotal === monthTotal);
console.log('MonthStats the max is: ', maxMonthTotal, ' on the position: ', maxMonthTotalIndex + 1)
console.log('MonthStats totals', monthTotals);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With