在javascript

时间:2018-05-18 12:35:35

标签: javascript

数据对象包含以下信息

var data =

    [{ creation_timestamp_dt: "2018-04-06T12:08:40Z", id: "110c983b-acd7-48a8-ba35-e4d0b17dfa3d" }
        , { creation_timestamp_dt: "2018-04-06T12:08:40Z", id: "c144e409-c81c-470f-a130-fb00018c32a5" }
        , { creation_timestamp_dt: "2018-04-12T20:49:35Z", id: "8a9efac8-1529-4355-a66c-0728a93c5341" }
        , { creation_timestamp_dt: "2018-04-12T20:49:35Z", id: "949317c5-200c-44fa-84bc-4630fc93ab71" }
        , { creation_timestamp_dt: "2018-04-13T16:17:43Z", id: "6c40778e-9033-4afa-83ee-ecbbf1387069" }
        , { creation_timestamp_dt: "2018-04-13T16:17:43Z", id: "24d8ad2a-d1f2-4abe-9ec3-03be46eb76bf" }
        , { creation_timestamp_dt: "2018-04-14T15:57:40Z", id: "f9b9fa3e-d62f-49f5-a318-917b889c2320" }
        , { creation_timestamp_dt: "2018-04-14T15:57:40Z", id: "382ab411-b1c8-4f33-ac9f-b1b123d0fa23" }
        , { creation_timestamp_dt: "2018-04-27T16:28:58Z", id: "057e37db-edc9-48ca-83a0-0bafc8d93d6b" }
        , { creation_timestamp_dt: "2018-04-27T16:28:58Z", id: "ac5de29b-2552-428e-ab1c-4991597a50ae" }
        , { creation_timestamp_dt: "2018-05-02T12:08:23Z", id: "bbb18e83-0197-4e5e-be2f-fc3eb89f7242" }
        , { creation_timestamp_dt: "2018-05-02T12:08:23Z", id: "123a1917-ad19-45a9-8d2e-4187e6bf5b6d" }
        , { creation_timestamp_dt: "2018-05-10T17:10:46Z", id: "5cb5c197-88df-4247-b5ee-f5e00f3e89a2" }
        , { creation_timestamp_dt: "2018-05-10T17:10:46Z", id: "aa70ba56-8128-410d-b5cf-4ad32abc3d8a" }
        , { creation_timestamp_dt: "2018-05-10T17:10:46Z", id: "613d8a61-d889-4642-a7de-a9390e97c4b3" }
        , { creation_timestamp_dt: "2018-05-15T15:54:43Z", id: "bfbf2a09-6baa-4f72-b1d8-87fc5256093c" }
        , { creation_timestamp_dt: "2018-05-15T15:54:43Z", id: "5cd53add-68ed-41c5-b3ee-e844c6d940ea" }
        , { creation_timestamp_dt: "2018-05-16T10:41:17Z", id: "7f363766-77b3-4a7b-a539-b30d280e98ac" }
        , { creation_timestamp_dt: "2018-05-16T10:41:17Z", id: "a8c9c754-65c9-48e5-a454-c1e98334b850" }
        , { creation_timestamp_dt: "2018-05-16T10:45:18Z", id: "cc37be99-d0fb-42f9-a49a-199962beb988" }
        , { creation_timestamp_dt: "2018-05-16T10:45:18Z", id: "a3336588-31dd-4864-9ca7-f651cd7457a4" }]

var counts = {};
for (var i = 0; i < data.length; i++) {
    counts[data[i].creation_timestamp_dt] = 1 + (counts[data[i].creation_timestamp_dt] || 0);
}
// 2018-04-06T12:08:40Z: 2    {timestamp: "2018-04-06T12:08:40Z",posts:2}
console.log(counts);

/*
    Getting below output
    2018-04-06T12:08:40Z:2
    2018-04-12T20:49:35Z:2
    2018-04-13T16:17:43Z:2
    2018-04-14T15:57:40Z:2
    2018-04-27T16:28:58Z:2
    2018-05-02T12:08:23Z:2
    2018-05-10T17:10:46Z:3
    2018-05-15T15:54:43Z:2
    2018-05-16T10:41:17Z:2
    2018-05-16T10:45:18Z:2
*/

我需要输出如下

/*

[{timestamp: "2018-04-06T12:08:40Z",posts:2},{timestamp:"2018-04-12T20:49:35Z",posts:2},{timestamp:"2018-04-13T16:17:43Z",posts:2},{timestamp:"2018-04-14T15:57:40Z",posts:2},{timestamp:"2018-04-27T16:28:58Z",posts:2},{timestamp:"2018-05-02T12:08:23Z",posts:2},{timestamp:"2018-05-10T17:10:46Z",posts:3},{timestamp:"2018-05-15T15:54:43Z",posts:2},{timestamp:"2018-05-16T10:41:17Z",posts:2},{timestamp:"2018-05-16T10:45:18Z",posts:2}]

*/

1 个答案:

答案 0 :(得分:1)

您可以使用哈希表作为时间戳:

 const result = [], hash = {};

 for(const {creation_timestamp_dt, id} of data) {
   if(hash[creation_timestamp_dt]) {
     hash[creation_timestamp_dt].posts += 1;
   } else {
     result.push(hash[creation_timestamp_dt] = {
        creation_timestamp_dt,
        posts: 1
     });
  }
}