• 数据循环处理重组2


    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            var arr = [{
                "temperatures": [{
                    "legend": "A相温度",
                    "unit": null,
                    "yaxis": "21.1",
                    "xaxis": "11"
                }, {
                    "legend": "B相温度",
                    "unit": null,
                    "yaxis": "21.8",
                    "xaxis": "11"
                }, {
                    "legend": "C相温度",
                    "unit": null,
                    "yaxis": "20.9",
                    "xaxis": "11"
                }, {
                    "legend": "N相温度",
                    "unit": null,
                    "yaxis": "21.4",
                    "xaxis": "11"
                }],
                "voltages": [{
                    "legend": "A相电压",
                    "unit": null,
                    "yaxis": "242.0",
                    "xaxis": "11"
                }, {
                    "legend": "B相电压",
                    "unit": null,
                    "yaxis": "240.0",
                    "xaxis": "11"
                }, {
                    "legend": "C相电压",
                    "unit": null,
                    "yaxis": "248.0",
                    "xaxis": "11"
                }],
                "currents": [{
                    "legend": "A相电流",
                    "unit": null,
                    "yaxis": "9.9",
                    "xaxis": "11"
                }, {
                    "legend": "B相电流",
                    "unit": null,
                    "yaxis": "17.2",
                    "xaxis": "11"
                }, {
                    "legend": "C相电流",
                    "unit": null,
                    "yaxis": "1.5",
                    "xaxis": "11"
                }],
            }, {
                "temperatures": [{
                    "legend": "A相温度",
                    "unit": null,
                    "yaxis": "21.2",
                    "xaxis": "11"
                }, {
                    "legend": "B相温度",
                    "unit": null,
                    "yaxis": "21.9",
                    "xaxis": "11"
                }, {
                    "legend": "C相温度",
                    "unit": null,
                    "yaxis": "21.1",
                    "xaxis": "11"
                }, {
                    "legend": "N相温度",
                    "unit": null,
                    "yaxis": "21.5",
                    "xaxis": "11"
                }],
                "voltages": [{
                    "legend": "A相电压",
                    "unit": null,
                    "yaxis": "242.0",
                    "xaxis": "11"
                }, {
                    "legend": "B相电压",
                    "unit": null,
                    "yaxis": "240.0",
                    "xaxis": "11"
                }, {
                    "legend": "C相电压",
                    "unit": null,
                    "yaxis": "248.0",
                    "xaxis": "11"
                }],
                "currents": [{
                    "legend": "A相电流",
                    "unit": null,
                    "yaxis": "9.9",
                    "xaxis": "11"
                }, {
                    "legend": "B相电流",
                    "unit": null,
                    "yaxis": "17.2",
                    "xaxis": "11"
                }, {
                    "legend": "C相电流",
                    "unit": null,
                    "yaxis": "1.5",
                    "xaxis": "11"
                }]
            }];
            // console.log(arr);
    
            // var final=[];  //最终要生成的数组
            // // 把type和name加进去
            // for(var i=0;i<arr[0].temperatures.length;i++){
            //     final[i]={};
            //     final[i].type='line'
            //     final[i].name=arr[0].temperatures[i].legend
            // }
            // // console.log(final)
            // //先把data需要用到的数据存起来
            // var dataArr=[]
            // for(var i=0;i<final.length;i++){
            //     dataArr[i]=[]
            // }
            // // console.log(dataArr)  //得到4个空数组
            // arr.forEach(item=>{
            //     item.temperatures.forEach((sonItem,index)=>{
            //         dataArr[index].push(sonItem.yaxis)
            //     })
            // })
            // // console.log(dataArr)  //得到需要的数组
    
            // final.forEach((item,index)=>{
            //     item.data=dataArr[index]
            // })
            // console.log(final)  
    
            function makeArr(sourceArr,type,typeName){
                var final=[];
                var dataArr=[];
                for(var i=0;i<sourceArr[0][type].length;i++){
                    final[i]={};
                    final[i].type=typeName
                    final[i].name=sourceArr[0][type][i].legend
                }
                // console.log(final)
                var dataArr=[]
                for(var i=0;i<final.length;i++){
                    dataArr[i]=[]
                }
                // console.log(dataArr)
                sourceArr.forEach(item=>{
                    item[type].forEach((sonItem,index)=>{
                        dataArr[index].push(sonItem.yaxis)
                    })
                })
                // console.log(dataArr)
                final.forEach((item,index)=>{
                    item.data=dataArr[index]
                })
                return final
            }
            var test=makeArr(arr,'currents','line')
            console.log(test)
        </script>
    </body>
    
    </html>
  • 相关阅读:
    [USACO08FEB]酒店Hotel 线段树 BZOJ 1593
    有趣的数 zoj 月赛
    [ZJOI2008]生日聚会 BZOJ1037 dp
    借教室 差分+二分答案
    HackerRank
    旅行计划 记忆化搜索
    灾后重建 Floyd
    [USACO10OCT]湖计数Lake Counting 联通块
    [TJOI2013]循环格 费用流 BZOJ3171
    高斯消元
  • 原文地址:https://www.cnblogs.com/Byme/p/10191713.html
Copyright © 2020-2023  润新知