Table longitudinal collated data set processing

About vertical table, a little explain:

Is the background for the property name can not be determined how much the use of KEY, VALUE to represent the attribute name, last added time, you can add the number and types
such as:

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":"成功"
            }

I would like to sort out the characteristics of each person is unique and each characteristic is to form KEY = VALUE to tip better direct access;

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"
    }
]

Guess you like

Origin www.cnblogs.com/lulianlian/p/11763448.html