欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数据循环处理重组2

程序员文章站 2022-04-13 15:25:57
Document ......
<!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>