使用lodash mapreduce更新集合的集合

时间:2019-04-17 20:09:17

标签: javascript

我有以下收集,我需要将其发送到数据表,并且它将不接受收集。因此,我需要弄平结构并尝试用lodash进行操作,但收效甚微。

我的最初收藏

{
    "campaigns": [
      {
        "name": "1st Campaign",
        "campaignType": "Test Campaign",
        "startDate": "2019/04/03",
        "endDate": "2019/04/30",
        "price": 1,
        "books": [
          {
            "title": "\"If They Move . . . Kill 'Em!\"",
            "bookdata": {
              "batch": 2
            }
          },
          {
            "title": "!Click Song",
            "bookdata": {
              "batch": null
            }
          }
        ]
      },
      {
        "name": "2nd cam",
        "campaignType": "test type",
        "startDate": "2019/04/10",
        "endDate": "2019/04/10",
        "price": 2.99,
        "retailers": null,
        "comments": null,
        "books": [
          {
            "title": "\"Johnny, We Hardly Knew Ye\"",
            "bookdata": {
              "batch": null
            }
          },
          {
            "title": "'A Very Fine Commander'",
            "bookdata": {
              "batch": 2
            }
          }
        ]
      }
    ]
  }

对于每个广告系列,我都需要像下面这样的结构,其中,广告系列中每本书的书名都以逗号分隔

   {
        "campaigns": [
          {
            "name": "1st Campaign",
            "campaignType": "Test Campaign",
            "startDate": "2019/04/03",
            "endDate": "2019/04/30",
            "price": 1,
            "booksname": "\"If They Move . . . Kill 'Em!\"","!Click Song"
            "books": [
              {
                "title": "\"If They Move . . . Kill 'Em!\"",
                "bookdata": {
                  "batch": 2
                }
              },
              {
                "title": "!Click Song",
                "bookdata": {
                  "batch": null
                }
              }
            ]
          },
          {
            "name": "2nd cam",
            "campaignType": "test type",
            "startDate": "2019/04/10",
            "endDate": "2019/04/10",
            "price": 2.99,
            "retailers": null,
            "comments": null,
            "books": [
              {
                "title": "\"Johnny, We Hardly Knew Ye\"",
                "bookdata": {
                  "batch": null
                }
              },
              {
                "title": "'A Very Fine Commander'",
                "bookdata": {
                  "batch": 2
                }
              }
            ]
          }
        ]
      }

1 个答案:

答案 0 :(得分:1)

我能够使用

                        data.campaigns.map((item) => {
                            item.Booksname = _.map(item.books, 'title').join(',');
                        })