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

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

后端返回的数组是这样的

"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日