实现前端从后端返回的json数据中的某个属性合并成数组

代码

后端返回的数据格式为

{
"success": true,
"data": [
{
"year": 2020,
"month": 1,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2020,
"month": 2,
"charge": 29000,
"discharge": 29000,
"temp": 36
},
{
"year": 2020,
"month": 3,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2020,
"month": 4,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2020,
"month": 5,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2020,
"month": 6,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2020,
"month": 7,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2020,
"month": 8,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2020,
"month": 9,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2020,
"month": 10,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2020,
"month": 11,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2020,
"month": 12,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 1,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 2,
"charge": 28000,
"discharge": 28000,
"temp": 36
},
{
"year": 2021,
"month": 3,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 4,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2021,
"month": 5,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 6,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2021,
"month": 7,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 8,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 9,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2021,
"month": 10,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2021,
"month": 11,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2021,
"month": 12,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 1,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 2,
"charge": 28000,
"discharge": 28000,
"temp": 36
},
{
"year": 2022,
"month": 3,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 4,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2022,
"month": 5,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 6,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2022,
"month": 7,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 8,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 9,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2022,
"month": 10,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2022,
"month": 11,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2022,
"month": 12,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2023,
"month": 1,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2023,
"month": 2,
"charge": 28000,
"discharge": 28000,
"temp": 36
},
{
"year": 2023,
"month": 3,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2023,
"month": 4,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2023,
"month": 5,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2023,
"month": 6,
"charge": 30000,
"discharge": 30000,
"temp": 36
},
{
"year": 2023,
"month": 7,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2023,
"month": 8,
"charge": 31000,
"discharge": 31000,
"temp": 36
},
{
"year": 2023,
"month": 9,
"charge": 20000,
"discharge": 20000,
"temp": 36
}
]
}

调用后端接口代码

  getAsyncTempCurrentData().then((response) => {
    console.log(response);

    const years = response.data.map((item) => item.year+'年'+item.month+'月') ;
    
    console.log(years);

    init();
  });


控制台输出years

[
    "2020年1月",
    "2020年2月",
    "2020年3月",
    "2020年4月",
    "2020年5月",
    "2020年6月",
    "2020年7月",
    "2020年8月",
    "2020年9月",
    "2020年10月",
    "2020年11月",
    "2020年12月",
    "2021年1月",
    "2021年2月",
    "2021年3月",
    "2021年4月",
    "2021年5月",
    "2021年6月",
    "2021年7月",
    "2021年8月",
    "2021年9月",
    "2021年10月",
    "2021年11月",
    "2021年12月",
    "2022年1月",
    "2022年2月",
    "2022年3月",
    "2022年4月",
    "2022年5月",
    "2022年6月",
    "2022年7月",
    "2022年8月",
    "2022年9月",
    "2022年10月",
    "2022年11月",
    "2022年12月",
    "2023年1月",
    "2023年2月",
    "2023年3月",
    "2023年4月",
    "2023年5月",
    "2023年6月",
    "2023年7月",
    "2023年8月",
    "2023年9月",
    "2023年10月"
]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值