Js处理后台json数组对象,遍历json数组对象,存入,排序

Js处理后台json数组对象,遍历json数组对象

json数据格式

var data = [
                {
                    "tag_id": "PC010402",
                    "tag_name": "烤箱1 电表KW",
                    "temp": [
                                {"dm": "2021-09-06 07:00","value": "0.00" },
                                {"dm": "2021-09-06 07:02","value": "0.00"},
                                {"dm": "2021-09-06 07:04","value": "0.00" }
                            ]
                }, 
                {
                    "tag_id": "PC010503",
                    "tag_name": "烤箱2 电表KW",
                    "temp": [
                                {"dm": "2021-09-06 07:00","value": "0.00" },
                                {"dm": "2021-09-06 07:02","value": "0.00"},
                                {"dm": "2021-09-06 07:04","value": "0.00" }
                            ]
                }, 
                {
                    "tag_id": "PC012203",
                    "tag_name": "烤箱A1 电表KW",
                    "temp": [
                                {"dm": "2021-09-06 07:00","value": "0.00" },
                                {"dm": "2021-09-06 07:02","value": "0.00"},
                                {"dm": "2021-09-06 07:04","value": "0.00" }
                            ]
                }
            ]

遍历json数组对象,进行存取


       var mydata =[];
      
        //后台数据
        if (data) {
           console.log("原json数据=" + JSON.stringify(data)+"  长度="+data.length);
  
            var dd={};
              for(var i = 0; i<data.length; i++){
                var dd={};
                var temp =[];
              //  遍历temp对象的下的所有属性,存入temp数组中
                    for(var j=1; j< data[i].temp.length; j++){
                        
                         dd ={
                            dm: data[i].temp[j].dm,
                            value: data[i].temp[j].value
                        }

                    //  alert("tag_id:"+data[i].tag_id+"tag_name:"+data[i].tag_name + "temp:"+data[i].temp);
                    
                  temp.push(dd);
                    }
                
                    if(data[i].tag_name ==="除菌烤箱 电表KW"){

                    }else if(data[i].tag_name ==="杀菌2 电表KW"){
                        //取出杀菌2 电表KW数组对象,最后面存入数组
                        var datass ={
                            tag_id: data[i].tag_id,
                            tag_name: data[i].tag_name,
                            temp: temp
                        }    

                    }else{
      
                        var datas ={
                            tag_id: data[i].tag_id,
                            tag_name: data[i].tag_name,
                            temp: temp
    
                        }
                        //存入mydata数组对象中
                        mydata.push(datas)
                    }

                }
           
                //把杀菌2 电表KW数组 添加进去,完成排序显示
              mydata.push(datass)
            console.log("login=" + JSON.stringify(mydata));
       }
上一篇:记录mysql迁移达梦全过程


下一篇:Adaptive AUTOSAR 学习笔记 14 - 车辆诊断