标签:Node 转换成 month judge 分组 result year date let
// 对时间戳按日期进行分组
let moment = require('moment')
let timestamp_array = [
1645059333000,
1613523333000,
1297904133000,
1298076933000,
1582073733000,
1645318535000,
1647389537000
]
let result = {}
for (let index in timestamp_array) {
let timestamp = timestamp_array[index]
let date = moment(timestamp).format("YYYY-MM-DD");
// let date = moment(timestamp).format("YYYY-MM");
// console.log(date);
let date_split_array = date.split('-')
// console.log(date_split_array);
// 先判断年,之后判断月份
let year = date_split_array[0]
let month = date_split_array[1]
let judge_year = result.hasOwnProperty(year)
if (judge_year == false) {
result[year] = {}
// result[year][date] = data
judge_month(year, month,date)
} else {
// result[year][date] = data
judge_month(year, month,date)
}
}
console.log(result);
function judge_month(year,month,date) {
/*
year: 字典中的年
month:
*/
let judge_month = result[year].hasOwnProperty(month)
if(judge_month == false){
result[year][month] = {}
result[year][month][date] = date
}else{
result[year][month][date] = date
}
}
运行结果如下所示
标签:Node,转换成,month,judge,分组,result,year,date,let 来源: https://www.cnblogs.com/leoych/p/15898900.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。