Lodash group通过React

时间:2017-11-16 07:26:34

标签: javascript reactjs lodash

继续Lodash groupBy with moment按日期(例如今天,本周,月等)对数组进行分组,使用胖箭头功能和时刻我现在尝试启用if then else(或三元)只启用如果sort参数(从状态传入)实际上是一个日期。

这就是我现在所拥有的......

     let groupby = datum => {
        if (this.state.sortGroup ='updatedAt') {
          return determineGroup(moment(groupProp(datum)));
        } else {
          this.state.sortGroup
        }
      }

      //console log point 1 -  shows state change

      groupedData = _
      .chain(this.props.records)
      .groupBy(groupby)
      .map(function(val, key) {
          return {
              group: key,
              records: val
          };
      })
      .value()

      //console log point 2 -  no state change

它按照分组的createdAt完美排序,但当我把if then else功能放入其中时,不会被任何其他道具排序。同样有趣的是,在日志点2,它没有显示状态的变化。

这是我做错了什么,影响if then else函数中的状态或差代码?

1 个答案:

答案 0 :(得分:1)

我假设outputConfig是指数据上属性的名称,在这种情况下,我认为你需要更像这样的东西:



sortGroup

const data = [{
  campaign: "Charles",
  company_ID: "1",
  coreURL: "http://www.test77.com",
  createdAt: "2017-11-06T20:45:56.931Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-09-06T20:45:56.931Z",
  _id: "6gsb42PSSJt7PgsDG"
}, {
  campaign: "Charles",
  company_ID: "1",
  coreURL: "http://www.test66,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-10-06T20:46:27.744Z",
  _id: "Md4wCnEsrQrWApnLS"
}, {
  campaign: "Gary",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-07-06T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-15T03:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-03T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-13T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-09T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}];

const groupProp = _.property('updatedAt');

let determineGroup = value => {
  // remove '2017-11-15' to actually use current date 
  const now = moment('2017-11-15T10:00:03Z').startOf('day');

  if (value.isSame(now, 'day')) {
    return 'today';
  }
  if (value.isAfter(now.clone().subtract(7, 'days').startOf('day'))) {
    return 'this week';
  }
  if (value.isSame(now, 'month')) {
    return 'this month';
  }
  return value.format('MM');
};

this.state = {
  sortGroup: 'updatedAt'
};

let groupby = datum => {
  const groupValue = datum[this.state.sortGroup];

  if (this.state.sortGroup === 'updatedAt') {
    return determineGroup(moment(groupValue));
  } else {
    return groupValue;
  }
}

let groupedData = _
  .chain(data)
  .groupBy(groupby)
  .value()

console.log(groupedData);