从对象数组中获取日期的最大值。

时间:2022-02-17 19:36:51

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

在一个反应。js组件,我有这个数组:

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).

我想要得到的是特定日期的值的总和(我在我的项目中使用Moment)。

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.

这样做的结果是7000,而不是8500。

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

我也可以使用Ramda方法,但是找不到合适的方法。

3 个解决方案

#1


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.

您可以使用数组#reduce来求和相同日期的对象的值。然后用数学。使用Object.values()实现最大值。

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);

#2


2  

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 combination 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)));

#3


0  

This answer is using moment js .

这个答案是用力矩js。

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

我正在创建一个对象,其中日期作为键,值作为这些日期的累积值。

While creating the object , I compare 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()

#1


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.

您可以使用数组#reduce来求和相同日期的对象的值。然后用数学。使用Object.values()实现最大值。

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);

#2


2  

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 combination 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)));

#3


0  

This answer is using moment js .

这个答案是用力矩js。

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

我正在创建一个对象,其中日期作为键,值作为这些日期的累积值。

While creating the object , I compare 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()