nodejs数据统计 展示本年的数据并以月份为横坐标

这里写目录标题

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, '')) // 分割出去第一位为0的
      arr.push({
        mounth: value,
        count: countMap[key],
        tourist: touristMap[key]
      })
    }
const oneData = []
    for (let t = 1; t <= 12; t++) { // 循环12个月
      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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值