1. 程式人生 > >數組中的對象的特征值提取生成新對象實現方法

數組中的對象的特征值提取生成新對象實現方法

span 當前 特征 約定 col 可視化 name code lena

最近要做一個可視化的SQL語句生成功能視圖,

項目中遇到一個JSON保存後還原的問題,由於保存之前和後臺溝通約定好保存的JSON格式,所以在還原的時候,就要按照保存的格式來進行逆向解析。

首先來看一下保存的JSON內容

var oldObj= [
      {
        "fieldName": "上班時間1",
        "fieldId": "working_hours_1",
        "fieldType": "time",
        "parentTableId": "class_table",
        "parentTableName": "班別表",
        
"order": 0, "checked": false }, { "fieldName": "下班時間1", "fieldId": "off_hours_1", "fieldType": "time", "parentTableId": "class_table", "parentTableName": "班別表", "order": 0, "checked": false }, { "fieldName": "上班時間2",
"fieldId": "working_hours_2", "fieldType": "time", "parentTableId": "class_table", "parentTableName": "班別表", "order": 0, "checked": false }, { "fieldName": "下班時間2", "fieldId": "off_hours_2", "fieldType": "time", "parentTableId": "class_table",
"parentTableName": "班別表", "order": 0, "checked": false }, { "fieldName": "姓名", "fieldId": "name", "fieldType": "text", "parentTableId": "punch_card_record", "parentTableName": "打卡記錄", "order": 0, "checked": false }, { "fieldName": "工號", "fieldId": "job_number", "fieldType": "text", "parentTableId": "punch_card_record", "parentTableName": "打卡記錄", "order": 0, "checked": false }, { "fieldName": "打卡時間", "fieldId": "punch_time", "fieldType": "datetime", "parentTableId": "punch_card_record", "parentTableName": "打卡記錄", "order": 0, "checked": false } ],

註:請忽略對象中的order,checked字段。

我們姑且將oldObj數組中的每個obj稱之為字段, 可以看到字段parentTableId 是有出現重復的情況的,

而我們要將這個數組解析成一個以 parentTableId 為特征值的新對象。

var newObj = [
    {
        "parentFormId": "class_table",
        "parentFormName": "班別表",
        "fields": [
            {
                "fieldName": "上班時間1",
                "fieldId": "working_hours_1",
                "fieldType": "time",
                "parentTableId": "class_table",
                "parentTableName": "班別表",
                "order": 0,
                "checked": false
            },
            {
                "fieldName": "下班時間1",
                "fieldId": "off_hours_1",
                "fieldType": "time",
                "parentTableId": "class_table",
                "parentTableName": "班別表",
                "order": 0,
                "checked": false
            },
            {
                "fieldName": "上班時間2",
                "fieldId": "working_hours_2",
                "fieldType": "time",
                "parentTableId": "class_table",
                "parentTableName": "班別表",
                "order": 0,
                "checked": false
            },
            {
                "fieldName": "下班時間2",
                "fieldId": "off_hours_2",
                "fieldType": "time",
                "parentTableId": "class_table",
                "parentTableName": "班別表",
                "order": 0,
                "checked": false
            }
        ]
    },
    {
        "parentFormId": "punch_card_record",
        "parentFormName": "打卡記錄",
        "fields": [
            {
                "fieldName": "姓名",
                "fieldId": "name",
                "fieldType": "text",
                "parentTableId": "punch_card_record",
                "parentTableName": "打卡記錄",
                "order": 0,
                "checked": false
            },
            {
                "fieldName": "工號",
                "fieldId": "job_number",
                "fieldType": "text",
                "parentTableId": "punch_card_record",
                "parentTableName": "打卡記錄",
                "order": 0,
                "checked": false
            },
            {
                "fieldName": "打卡時間",
                "fieldId": "punch_time",
                "fieldType": "datetime",
                "parentTableId": "punch_card_record",
                "parentTableName": "打卡記錄",
                "order": 0,
                "checked": false
            }
        ]
    }
]

註:請忽略新對象中的 parentTableIdparentTableName,order,checked 字段, 因為不影響我們的目的,為了方便直接就把對象push進去了。

可以看到,新數組對象中,將字段中的parentTable的ID,Name都提取了出來,並且將這些字段push到了一個newObj數組裏面。
那麽這個實現的結果,是怎麽達成的呢,且看一下代碼:

//新建一個用於存放特征值的數組
let parentTableIdArray = [];
//遍歷舊數組對象
for(let item of oldObj){
//獲取當前的對象parentTableId是否存在特征值數組
  let index = parentTableIdArray.indexOf(item.parentTableId)
//當前的對象parentTableId不存在特征值數組時
  if(index == -1){
//將當前的對象parentTableId加入到特征值數組
    parentFormIdArray.push(item.parentTableId)
//生成新對象
    let obj = {
      fields: [],
      parentFormId: item.parentTableId,
      parentFormName: item.parentTableName
    }
    obj.fields.push(item);
//插入新生成的對象
    this.newObj.push(obj);
  }
//當前的對象parentTableId存在特征值數組時
  else{
//在當前的對象parentTableId的位置插入新對象
    this.newObj[index][‘fields‘].push(item);
  }
}   

這個方法可以有效的提取數組內的對象特征值。如果需要多次調用,可以考慮封裝。

數組中的對象的特征值提取生成新對象實現方法