a543462378 2022-03-30 14:58 采纳率: 84.7%
浏览 92
已结题

怎么把后端返回的数组拆分

后端返回的数组是这样的

"data":[{
            "otherAmount": 816.68,
            "proportion": 21.34,
            "thisAmount": 221.6,
            "type": "生活垃圾"
        },
        {
            "otherAmount": 0.36,
            "proportion": 100,
            "thisAmount": 0,
            "type": "含油污水"
        },
        {
            "otherAmount": 7553,
            "proportion": 0.01,
            "thisAmount": 0.6,
            "type": "生活污水"
        }]

需要把它变成这样的数组

"allData":[{
            "lable":"对比数量"
            "amount": 816.68, // "otherAmount": 816.68,
            "proportion": 21.34,
            "type": "生活垃圾"
        },
        {
            "lable":"对比数量"
            "Amount": 0.36, // "otherAmount": 0.36,
            "proportion": 100,
            "type": "含油污水"
        },
        {
            "lable":"对比数量"
            "amount": 7553, // "otherAmount": 7553,
            "proportion": 0.01,
            "type": "生活污水"
        },
        {
            "lable":"选择数量"
            "proportion": 21.34,
            "amount": 221.6, // "thisAmount": 221.6,
            "type": "生活垃圾"
        },
        {
            "lable":"选择数量"
            "proportion": 100,
            "amount": 0, // "thisAmount": 0,
            "type": "含油污水"
        },
        {
            "lable":"选择数量"
            "proportion": 0.01,
            "amount": 0.6, // "thisAmount": 0.6,
            "type": "生活污水"
        }]

就是把数组拆分一下,把thisAmount和otherAmount放入不同的新内容里面,同时给与lable

  • 写回答

2条回答 默认 最新

  • bdawn 2022-03-30 15:21
    关注
    
    let allData = data.map(item => {
        return {
            lable: '对比数量',
            amount: item.otherAmount,
            proportion: item.proportion,
            type: item.type,
        }
    })
    
    // allData
    // [
    //   {lable: '对比数量', amount: 816.68, proportion: 21.34, type: '对比数量'},
    //    {lable: '对比数量', amount: 0.36, proportion: 100, type: '对比数量'}
    // ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月10日
  • 已采纳回答 4月2日
  • 创建了问题 3月30日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?