使用groupBy和map来使用LoDash和Moment转换数据

Using groupBy and map to transform data using LoDash and Moment

本文关键字:LoDash Moment 转换 数据 groupBy map 使用      更新时间:2023-09-26

我对lodash没有经验,但我相信它可以帮助我将数据转换为所需的格式。我已经尝试了文档中描述的不同级别的方法,但我无法理解所有的东西。我看了SO,一些博客和文档。我已经尝试组合groupby和map,但我无法解决这个问题。我也不确定如何记录这些步骤。

这就是我想做的,我想把下面的数组变成后面的数组。有人能给我指个方向吗?

原始数据

var mockData = [
      {
        "notice_title": "Bad news",
        "notice_text": "Server is down!",
        "start_date": "2016-09-18T04:00:00Z"
      },
      {
        "notice_title": "Weekly Reminder",
        "notice_text": "Please read the assignment!",
        "start_date": "2016-09-18T04:00:00Z"
      },
      {
        "notice_title": "Sweet",
        "notice_text": "This morning, the new edition of our blog hit stands!",
        "start_date": "2016-09-19T04:00:00Z"
      },
      {
        "notice_title": "Yeah",
        "notice_text": "This is pretty cool",
        "start_date": "2016-09-19T04:00:00Z"
      }
所需数据

var newMockData = [
    { 
        "date": "JAN 18 2016", 
        "messages": [{
            "notice_title": "Bad news",
            "notice_text": "Server is down!",
            "start_date": "2016-09-18T04:00:00Z"
          },
          {
            "notice_title": "Weekly Reminder",
            "notice_text": "Please read the assignment!",
            "start_date": "2016-09-18T04:00:00Z"
          }],
        "date": "JAN 19 2016", 
        "messages": [{
            "notice_title": "Sweet",
            "notice_text": "This morning, the new edition of our blog hit stands!",
            "start_date": "2016-09-19T04:00:00Z"
          },
          {
            "notice_title": "Yeah",
            "notice_text": "This is pretty cool",
            "start_date": "2016-09-19T04:00:00Z"
          }]
}]

更新lodash

var result = _.chain(mockData)
      .groupBy(function(item) {
        return moment(item.start_date.substring(0,10)).format("MMM-DD-YYYY"); 
      })
      .map((value, key) => {
        return {
          date: key, 
          param: value
        }
      })
      .value();

我昨天确实回答了一个非常类似的问题,但是,我仍然会通过修改上一个问题来提供一个答案

var mockData = [
      {
        "notice_title": "Bad news",
        "notice_text": "Server is down!",
        "start_date": "2016-09-18T04:00:00Z"
      },
      {
        "notice_title": "Weekly Reminder",
        "notice_text": "Please read the assignment!",
        "start_date": "2016-09-18T04:00:00Z"
      },
      {
        "notice_title": "Sweet",
        "notice_text": "This morning, the new edition of our blog hit stands!",
        "start_date": "2016-08-19T04:00:00Z"
      },
      {
        "notice_title": "Yeah",
        "notice_text": "This is pretty cool",
        "start_date": "2016-09-19T04:00:00Z"
      }
]
var result = _.chain(mockData)
  .groupBy(datum => moment(datum.start_date).format("MMM DD YYYY").toLocaleUpperCase() )
  .map((messages, date) => ({ date, messages })) //using ES6 shorthand to generate the objects
  .value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>

这里的关键是,当使用_.groupBy时,您可以提供一个函数来定义如何将对象收集在一起。在本例中,使用Moment.js将消息的start_date格式化为月-日-年格式:

moment(datum.start_date).format("MMM DD YYYY")` 

这将解析消息的日期并以<Short month> <day> <year>格式输出。在文档中查看有关格式化的更多信息。然后将该值转换为大写以将"Sep"转换为"Sep"

第二件事就是在.map内部生成新的结构。因为所需的所有信息都已经以如下格式呈现

{
    "Sep 18 2016": [{
        "notice_title": "Bad news",
        "notice_text": "Server is down!",
        "start_date": "2016-09-18T04:00:00Z"
    }, {
        "notice_title": "Weekly Reminder",
        "notice_text": "Please read the assignment!",
        "start_date": "2016-09-18T04:00:00Z"
    }],
    "Aug 19 2016": [{
        "notice_title": "Sweet",
        "notice_text": "This morning, the new edition of our blog hit stands!",
        "start_date": "2016-08-19T04:00:00Z"
    }],
    "Sep 19 2016": [{
        "notice_title": "Yeah",
        "notice_text": "This is pretty cool",
        "start_date": "2016-09-19T04:00:00Z"
    }]
}

这是一个简单的问题,获取键并将其转换为属性和值并作为另一个属性添加。