简体   繁体   English

Lodash组通过React的胖箭头功能

[英]Lodash groupBy fat arrow function with React

Following on from Lodash groupBy with moment that groups an array by dates such as today, this week, month etc by using a fat arrow function and moment I am now trying to enable if then else (or ternary) that only enables this if the sort parameter (passed in from state) is actually a date. 接下来是Lodash groupBy,然后通过使用箭头功能和按日期将日期(例如今天,本周,月份等)对数组进行分组的时刻 ,现在我尝试启用if then else(或三元),仅当排序时才启用此功能参数(从状态传入)实际上是一个日期。

This is what I have at the moment... 这就是我目前所拥有的...

     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

It sorts by the grouped createdAt perfectly, but when I put the if then else functionality in it does not sort by any other prop. 它按分组的createdAt进行完美排序,但是当我放入if then else功能时,它不会按其他任何道具进行排序。 Also interestingly at log point 2 it does not show the state changing. 同样有趣的是,在日志点2上,它没有显示状态变化。

Is this something I am doing wrong that is affecting state or poor code in the if then else function? 这是我做错的事情吗,如果在if else函数中影响状态或不良代码?

I'm assuming sortGroup refers to the name of a property on the data, in which case, I think you need something more like this: 我假设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); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM