javascript - Get the max value for a date from an array of objects - Stack Overflow

In a React.js ponent, I have this array:const array = [{date: '2017-12-21T07:43:00Z',value: 7

In a React.js ponent, I have this array:

const array = [{
  date: '2017-12-21T07:43:00Z',
  value: 7000
}, {
  date: '2017-12-21T09:41:00Z',
  value: 1500,
}, {
  date: '2017-12-23T10:59:00Z',
  value: 2000
}]

What I want to get is the sum of the values for a specific date (I'm using Moment in my project).

From then on I can get the maximum value from the list, like this:

function getMaxValue () {
  const { data } = this.props
  return data.reduce((max, item) => item.value > max ? item.value : max, data[0].value)
}

By doing that, though, the result is 7000, instead of 8500.

I can use Ramda methods too, but can't find a suitable one.

In a React.js ponent, I have this array:

const array = [{
  date: '2017-12-21T07:43:00Z',
  value: 7000
}, {
  date: '2017-12-21T09:41:00Z',
  value: 1500,
}, {
  date: '2017-12-23T10:59:00Z',
  value: 2000
}]

What I want to get is the sum of the values for a specific date (I'm using Moment in my project).

From then on I can get the maximum value from the list, like this:

function getMaxValue () {
  const { data } = this.props
  return data.reduce((max, item) => item.value > max ? item.value : max, data[0].value)
}

By doing that, though, the result is 7000, instead of 8500.

I can use Ramda methods too, but can't find a suitable one.

Share Improve this question edited Apr 24, 2018 at 12:10 Mihai Alexandru-Ionut 48.5k14 gold badges105 silver badges132 bronze badges asked Apr 24, 2018 at 11:56 abpetkovabpetkov 9145 gold badges14 silver badges29 bronze badges 1
  • You can simply do this with native ES6 functionalities. Have a look at this answer. – Redu Commented Apr 24, 2018 at 12:18
Add a ment  | 

3 Answers 3

Reset to default 2

You can use array#reduce to sum the value of the object of the same date. Then use Math.max with Object.values() to get the maximum value.

const array = [{ date: '2017-12-21T07:43:00Z', value: 7000 }, { date: '2017-12-21T09:41:00Z', value: 1500, }, { date: '2017-12-23T10:59:00Z', value: 2000 }],
      result = array.reduce((r, {date, value}) => {
        date = date.substring(0,10);
        r[date] = (r[date] || 0) + value;
        return r;
      },{});
const maxValue = Math.max(...Object.values(result));
console.log(maxValue);

First of all, you have to group your array by date field using a hash structure.

Then, you just need to use Math.max method in bination with spread syntax in order to achieve your requirement.

const array = [{ date: '2017-12-21T07:43:00Z', value: 7000 }, { date: '2017-12-21T09:41:00Z', value: 1500, }, { date: '2017-12-23T10:59:00Z', value: 2000 }]

let grouped = [];
array.forEach(function (o) {
    let date = o.date.substring(0, 10);
    if (!this[date]) {
        this[date] = { date: date, value: 0 };
        grouped.push(this[date]);
    }
    this[date].value += o.value;
}, Object.create(null));

console.log(Math.max(...grouped.map(a => a.value)));

This answer is using moment js .

Iam creating an object with date as key and value as cumulative values of those dates.

While creating the object , I pare the max value and returning it

const array = [{
  date: '2017-12-21T07:43:00Z',
  value: 7000
}, {
  date: '2017-12-21T09:41:00Z',
  value: 1500,
}, {
  date: '2017-12-23T10:59:00Z',
  value: 2000
}]

function getMaxValue(){
 let res = {};
 let max = Number.MIN_VALUE
 array.forEach(obj => {
   let key = moment(obj.date).format('YYYY-MM-DD');
    res[key] = res[key]? res[key]+ obj.value: obj.value
   if(res[key] > max){
     max = res[key];
   }
 });

 return max;
}

getMaxValue()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744909827a4600479.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信