最近要做一个可视化的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 } ] } ]
注:请忽略新对象中的 parentTableId,parentTableName,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); } }
这个方法可以有效的提取数组内的对象特征值。如果需要多次调用,可以考虑封装。