zoukankan      html  css  js  c++  java
  • 纵表整理数据集合处理

    关于纵表,小小的说明一下:

    就是后台对于无法确定属性名有多少,采用KEY,VALUE来代表属性名,最后添加的时候,可以随意添加数量和种类
    比如:

    var list = {
    			    "code":"200",
    			    "data":[
    			        {
    			            "type":"XZ",
    			            "id":856,
    			            "profileKey":"name",
    			            "profileValue":"肖战",
    			        },
    			        {
    			            "type":"XZ",
    			            "id":854,
    			            "profileKey":"age",
    			            "profileValue":"18",
    			        },
    			        {
    			        	"type":'PYY',
    			            "id":853,
    			            "profileKey":"name",
    			            "profileValue":"彭于晏",
    			        },{
    			        	"type":"PYY",
    			            "id":852,
    			            "profileKey":"age",
    			            "profileValue":"20",
    			        }
    			    ],
    			    "msg":"成功"
    			}
    

    我想整理出来,每个人的特性都是唯一的,并且每个特性都是以KEY=VALUE的形式来,前端比较好直接取用;

    function getCollection(callback) {
    		let data = list.data;
    		let allArr = [];
    		let allObj = {};
    		let initObj = {
    			type: data[0].type,
    			[data[0].profileKey]: data[0].profileValue
    		}
    
    		for(var i = 1; i < data.length; i ++){
    			let currItem = data[i-1], nextItem = data[i];
    			if (currItem.type === nextItem.type) {
    				allObj[currItem.type] = Object.assign(initObj, {
    					[nextItem.profileKey]: nextItem.profileValue
    				})
    			}else {
    				initObj = {
    					type: nextItem.type,
    					[nextItem.profileKey]: nextItem.profileValue
    				}
    				continue;
    			}
    			
    		}
    		//获取list---数组形式
    	    for (var key in allObj) {
    	      allArr.push(allObj[key]);
    	    }
    
    	    callback(allObj,allArr);
    }
    
    
    //调用方法
    	    getCollection((resObj, resList) =>{
            //使用数据
    	    	console.log(resObj);
    	    	console.log(resList)
    	    })
    
    // 对象形式 --resObj                      
     {
       PXY:{
        age: "20"
        name: "彭于晏"
        type: "PXY"
      },
      XZ:{
          age: "18"
          name: "肖战"
          type: "XZ"
        }
     } 
    
    //数组形式 --- resList
    [
      {
        age: "20"
        name: "彭于晏"
        type: "PXY"
      },{
          age: "18"
          name: "肖战"
          type: "XZ"
        }
    ]
  • 相关阅读:
    regulation
    Java第三方类库
    python整个小服务器
    VsFTP出现500 OOPS: cannot change directory的解决办法
    Got error 28 from storage engine
    linux下ftp操作
    linux安装JDK
    Apache + Tomcat + Linux 集群和均衡负载 (Session 同步复制) 配置实
    怎么样才能使得PL/SQL Developer不显示系统表?
    sudo 用法
  • 原文地址:https://www.cnblogs.com/lulianlian/p/11763448.html
Copyright © 2011-2022 走看看