原始数据
let data=[
{
time:'2021-06-01 12:45:42',
state:'alarm'
},
{
time:'2021-06-01 16:45:42',
state:'alarm'
},
{
time:'2021-06-05 16:45:42',
state:'alarm'
},
]
需求数据:统计当月每日数量
[2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
方法
let data = res.data.data
console.log('月数据')
console.log(data)
// 获取年月,用于拼出自然日的日期
let day = new Date(this.choseDay)
let y = day.getFullYear()
let m = day.getMonth() + 1
m = m > 9 ? m : '0' + m
var allData = [] // 最终数组
let mAllDay = monthAllDay(this.choseDay) // 获取当月总天数(30)
for (let d = 1; d <= mAllDay; d++) { // 循环当月总天数
d = d > 9 ? d : '0' + d
let ziRanDate = `${y}-${m}-${d}` // 拼出自然日
var count = 0 //计数
for (let i = 0; i < data.length; i++) { // 循环报警数组
// console.log(data[i].starttime.substring(0, 10))
let gaoJingDate = data[i].starttime.substring(0, 10) // 截取告警日期
if (ziRanDate === gaoJingDate) { // 自然日与告警日期重合,计数增加
// console.log('告警日期')
// console.log(gaoJingDate)
count++
// console.log(count)
}
}
allData.push(count) // 计数push进数组
}
console.log('全部')
console.log(allData)