admin管理员组

文章数量:1313347

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?

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?

Share Improve this question edited Jan 22, 2020 at 4:18 Vectrobyte asked Jan 22, 2020 at 4:03 VectrobyteVectrobyte 1,48515 silver badges33 bronze badges 7
  • monthStats length will be 3? – Saurabh Agrawal Commented Jan 22, 2020 at 4:07
  • Yes, it will always be 3 – Vectrobyte Commented Jan 22, 2020 at 4:09
  • 1 "the sum of values of alternate data" what do you mean by "alternate"? From your example, it seems "all" is a more suitable word than "alternate". Did I get it wrong? – Stratubas Commented Jan 22, 2020 at 4:09
  • 1 @Stratubas I've updated the detail. Please check it above. – Vectrobyte Commented Jan 22, 2020 at 4:12
  • 1 @Vectrobyte 1) In javascript array index starts at 0. So, 5, 10, and 15 are at the 0-index. 2) the "failed" array, the 1st value at the 0-index is 15. The fifth value at 4-index is NOT 150. It is 0. So it appears that your maximum value is not 500, but 350. – jwatts1980 Commented Jan 22, 2020 at 4:15
 |  Show 2 more ments

5 Answers 5

Reset to default 6
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);

Try this:

 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 max = 0;
monthStats[0].data.map((v, i) => {
    let sum = [monthStats[0].data[i], monthStats[1].data[i], monthStats[2].data[i]].reduce((v1, v2) => {
        return parseInt(v1) + parseInt(v2);
    })
    if (sum > max) {
        max = sum;
    }
})
console.log(max)

My approach:

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'],
}];

const dataArrays = monthStats.map(type => type.data.map(str => parseFloat(str)));

let bestSum = 0;
let bestDay = 0;
for (let day = 0; day < dataArrays[0].length; day++) {
  const daySum = dataArrays.reduce((daySumAccumulator, typeData) => {
    return daySumAccumulator + typeData[day];
  }, 0);
  if (daySum > bestSum) {
    bestSum = daySum;
    bestDay = day;
  }
}
console.log('Best sum is', bestSum, 'and was found at day', bestDay);

You can map each data array to into its own array using .map() to separate it from your object. You can then zip all your data arrays together to give you arrays containing elements from the same index. Then, you can sum each of these zipped arrays using .map() and .reduce(). Finally, you can find the max by spreading the results into Math.max():

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'], }];

const zip = arrs => arrs[0].map((_,c)=>arrs.map(arr=>arr[c]));
const data = zip(monthStats.map(({data}) => data));
const sums = data.map(arr => arr.reduce((acc, n) => acc + +n, 0));
const biggest = Math.max(...sums);
console.log("Biggest sum is:", biggest, "at index:", sums.indexOf(biggest));

本文标签: (JavaScript) Get the sum of alternate values in array and get the maximum value from itStack Overflow