使用lodash格式化JSON数据

时间:2016-09-08 17:15:52

标签: javascript json lodash

我有一个JSON数据,格式如下:

活动

[
  {
    "id": 32,
    "poi_id": 1,
    "due_date": "2016-09-08T18:15:00.000Z",
    "items": [
      {
        "id": 21,
        "name": "Choluv jar : JAR",
        "activity_id": 32
      }
    ]
  },
  {
    "id": 30,
    "poi_id": 9,
    "due_date": "2016-09-14T18:15:00.000Z",
    "items": [
      {
        "id": 17,
        "name": "Bourbon Family : PKT",
        "activity_id": 30
      },
      {
        "id": 18,
        "name": "Choluv jar : JAR",
        "activity_id": 30
      }
    ]
  },
  {
    "id": 29,
    "poi_id": 1,
    "due_date": "2016-09-27T18:15:00.000Z",
    "items": [
      {
        "id": 16,
        "name": "Choluv jar : JAR",
        "activity_id": 29
      }
    ]
  }
]

我想使用 lodash 或只是 javascript 重新格式化这些数据,如下所示:

/*poi_id is the key*/

  "1": [{
    "id": 32,
    "poi_id": 1,
    "due_date": "2016-09-08T18:15:00.000Z",
    "items": {
      /*due_date is the key*/

      "2016-09-08T18:15:00.000Z": [{
        "id": 21,
        "name": "Choluv jar : JAR",
        "activity_id": 32
      }]
    }
  }, {
    "id": 29,
    "poi_id": 1,
    "due_date": "2016-09-27T18:15:00.000Z",
    "items": {
      "2016-09-27T18:15:00.000Z": [{
        "id": 16,
        "name": "Choluv jar : JAR",
        "activity_id": 29
      }]
    }
  }],

  "9": [{
    "id": 30,
    "poi_id": 9,
    "due_date": "2016-09-14T18:15:00.000Z",
    "items": {
      "2016-09-14T18:15:00.000Z": [{
        "id": 17,
        "name": "Bourbon Family : PKT",
        "activity_id": 30
      }, {
        "id": 18,
        "name": "Choluv jar : JAR",
        "activity_id": 30
      }]
    }
  }]

我想要的是将具有相同 poi_id 的数据放在一个集合下,并使用 poi_id key,并将项目设置为相同具有相同的 due_date

这是我到目前为止所做的事情:

let activityArray = {};
_.forEach(activities, (activityItem) => {
  if (!activityArray[activityItem.poi_id]) {
    activityArray[activityItem.poi_id] = [];
  }
  activityArray[activityItem.poi_id].push(activityItem);
  _.forEach(activityArray[activityItem.poi_id], (value, key) => {
    activityArray[activityItem.poi_id][key].items.unshift(activityArray[activityItem.poi_id][key].due_date);
  });
});

这就是我得到的:

  "1": [{
    "id": 32,
    "poi_id": 1,
    "due_date": "2016-09-08T18:15:00.000Z",

    /*unShift added due_date twice here, I want here key value pair*/

    "items": [
      "2016-09-08T18:15:00.000Z",
      "2016-09-08T18:15:00.000Z", {
        "id": 21,
        "name": "Choluv jar : JAR",
        "activity_id": 32
      }
    ]
  }, {
    "id": 29,
    "poi_id": 1,
    "due_date": "2016-09-27T18:15:00.000Z",
    "items": [
      "2016-09-27T18:15:00.000Z", {
        "id": 16,
        "name": "Choluv jar : JAR",
        "activity_id": 29
      }
    ]
  }],
  "9": [{
    "id": 30,
    "poi_id": 9,
    "due_date": "2016-09-14T18:15:00.000Z",
    "items": [
      "2016-09-14T18:15:00.000Z", {
        "id": 17,
        "name": "Bourbon Family : PKT",
        "activity_id": 30
      }, {
        "id": 18,
        "name": "Choluv jar : JAR",
        "activity_id": 30
      }
    ]
  }]

我也试过其他方法,但不能像我期待的那样。

请在这里指导我。

感谢。

1 个答案:

答案 0 :(得分:1)

普通Javascript中的紧凑解决方案,其中对象为项数组的哈希值。

var activities = [{ "id": 32, "poi_id": 1, "due_date": "2016-09-08T18:15:00.000Z", "items": [{ "id": 21, "name": "Choluv jar : JAR", "activity_id": 32 }] }, { "id": 30, "poi_id": 9, "due_date": "2016-09-14T18:15:00.000Z", "items": [{ "id": 17, "name": "Bourbon Family : PKT", "activity_id": 30 }, { "id": 18, "name": "Choluv jar : JAR", "activity_id": 30 }] }, { "id": 29, "poi_id": 1, "due_date": "2016-09-27T18:15:00.000Z", "items": [{ "id": 16, "name": "Choluv jar : JAR", "activity_id": 29 }] }],
    hash = {},
    grouped = {};

activities.forEach(a => {
    hash[a.poi_id] = hash[a.poi_id] || {};
    hash[a.poi_id][a.due_date] = hash[a.poi_id][a.due_date] || [];
    grouped[a.poi_id] = grouped[a.poi_id] || [];
    grouped[a.poi_id].push({
        id: a.id,
        poi_id: a.poi_id,
        due_date: a.due_date,
        items: { [a.due_date]: hash[a.poi_id][a.due_date] }
    });
    a.items.forEach(b => hash[a.poi_id][a.due_date].push(b));
});

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }