簡體   English   中英

使用groupBy和map使用LoDash和Moment轉換數據

[英]Using groupBy and map to transform data using LoDash and Moment

我對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"
    }]
}

獲取密鑰並將它們轉換為屬性和值並作為另一個屬性添加是一件簡單的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM