在JavaScript中如何将时间戳统计结果对象转换为指定数组格式?
转换统计结果为指定数组格式的JavaScript方案
原始数据与现有统计逻辑
首先,你已经有了包含creation_timestamp_dt和id的原始数据,以及统计各时间戳条目数量的代码:
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); }
这段代码运行后,counts对象会存储每个时间戳对应的条目数量。
转换为目标数组格式
要把counts转换成你需要的{timestamp: "...", posts: N}格式的数组,这里有两种实用方案:
方案一:ES6简洁写法(推荐)
利用Object.entries()和箭头函数快速转换,代码简洁易懂:
// 转换counts为目标数组 var result = Object.entries(counts).map(([timestamp, posts]) => ({ timestamp, posts })); // 输出结果 console.log(result);
代码解释
Object.entries(counts):将counts对象转换为二维数组,每个子数组格式为[时间戳, 对应数量],比如["2018-04-06T12:08:40Z", 2]。.map(([timestamp, posts]) => ({ timestamp, posts })):遍历二维数组,解构出时间戳和数量,用对象字面量简写语法生成目标格式的对象。
方案二:兼容旧环境写法
如果你的运行环境不支持ES6语法,用传统循环写法也能实现:
var result = []; for (var timestamp in counts) { // 确保只处理对象自身的属性,避免原型链污染 if (counts.hasOwnProperty(timestamp)) { result.push({ timestamp: timestamp, posts: counts[timestamp] }); } } console.log(result);
最终输出结果
两种方案都会得到你需要的数组:
[ {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} ]
内容的提问来源于stack exchange,提问作者Harsh
相关产品推荐
相关产品推荐

