1.统计图展示

2.逻辑实现
const data = [
{
time: 2021-1-11,
count: 2,
tourist:4
},
{
time: 2021-1-11,
count: 2,
tourist:4
},
{
time: 2021-2-11,
count: 2,
tourist:4
}]
const touristMap = {}
const countMap = {}
for (const item of data) {
const regexpRes = /(\d{4})-(\d{2})-(\d{2})/.exec(item.time)
if (parseInt(regexpRes[1]) === new Date().getFullYear())
{
const mounth = regexpRes[2]
if (!touristMap[mounth]) {
touristMap[mounth] = item.tourist
} else {
touristMap[mounth] += item.tourist
}
if (!countMap[mounth]) {
countMap[mounth] = item.count
} else {
countMap[mounth] += item.count
}
}
}
console.log(touristMap)
const arr = []
for (const key in touristMap) {
const value = parseInt(key.replace(/\b(0+)/gi, ''))
arr.push({
mounth: value,
count: countMap[key],
tourist: touristMap[key]
})
}
const oneData = []
for (let t = 1; t <= 12; t++) {
oneData.push({ mounth: t, count: 0, tourist: 0 })
}
oneData.map(reward => {
arr.map(towReward => {
if (reward.mounth === towReward.mounth) {
reward.count = towReward.count
reward.tourist = towReward.tourist
}
return reward
})
})
return oneData