该reduce函数的类型应该是什么

时间:2019-09-09 11:12:02

标签: javascript node.js typescript lodash

 const merged_health_attributes = _.toPairs(sorted_attributes_by_definition_id).reduce((acc,[had_id, v]: [RecordId, any]) => {
  return acc;
}, {});

我在这里定义了[RecordId,any]类型。但我希望类型有意义。我需要将任何类型更改为某种意义上的完整类型。

const sorted_attributes_by_definition_id = sorted_health_attributes_to_merge.reduce((acc, attr) => {
  const had_id = attr.relationships.health_attribute_definition.data.id;
  if (!acc[had_id]) {
    // eslint-disable-next-line nurx/no-param-reassign
    acc[had_id] = [];
  }
  acc[had_id].push(attr);
  return acc;
}, {});

// group the attrs for each definition into the format: {recent:HealthAttribute, prev:[HealthAttributes]}
const merged_health_attributes = _.toPairs(sorted_attributes_by_definition_id).reduce((acc,[had_id, v]: [RecordId, any]) => {
  // if the most recent is older than the selected request/renewal by more than a day, don't include it as 'recent'
  // we don't want to old attrs that are time senstitive (like pregnancy, cancer) to come up as 'recent'

  let recent = null;
  let prev = [];
  // if v[0] exists, we want to make sure it's recent enough to display as 'recent'
  if (v[0]) {
    const recent_created_at = moment(v[0].attributes.created_at);
    const source_created_at = moment(request_or_renewal.attributes.created_at);

    // if the source is younger than the attributes by more than a day, don't include them as recent
    if (source_created_at.diff(recent_created_at, 'days') > 1) {
      prev = v;
    } else {
      recent = v[0];
      prev = v.slice(1);
    }
  }

  const to_push = { recent, prev };
  if (!acc[had_id]) {
    // eslint-disable-next-line nurx/no-param-reassign
    acc[had_id] = [];
  }
  acc[had_id].push(to_push);

  return acc;
}, {});

return merged_health_attributes;

}

这是我正在使用reduce的Whole函数

0 个答案:

没有答案