1. 对象里面添加数组对象

发送post提交的接口参数
{"f5768c21": ["subf642c26"],"f5768c102": ["subf761c10","subf761c13"]}
接口JSON数据返回
[{"atrName": "subf642c26","columnName": "subf642c26","formId": 5768,"dataTypeId": 1,"showName": "子表单第一个-单行文本","parentType": "subform","parentColumnName": "f5768c21","type": "input"},{"atrName": "subf761c10","columnName": "subf761c10","formId": 5768,"dataTypeId": 1,"showName": "子表单第二个-单行文本","parentType": "subform","parentColumnName": "f5768c102","type": "input"},{"atrName": "subf761c13","columnName": "subf761c13","formId": 5768,"dataTypeId": 3,"showName": "子表单第二个-数字","parentType": "subform","parentColumnName": "f5768c102","type": "countNumber"},{"formId": 5768,"atrName": "username","itemType": 2,"showName": "提交人","formName": "综合组件","dataTypeId": 1,"options": null,"id": "5768_5","type": "submitPerson","columnName": "user_table_f5768.username"},{"formId": 5768,"atrName": "createTimeF5768","itemType": 2,"showName": "提交时间","formName": "综合组件","dataTypeId": 4,"options": null,"id": "5768_6","type": "submitTime","columnName": "create_time_f5768"},{"formId": 5768,"atrName": "updateTimeF5768","itemType": 2,"showName": "更新时间","formName": "综合组件","dataTypeId": 4,"options": null,"id": "5768_7","type": "UpdateTime","columnName": "update_time_f5768"}]
代码
首先判断是否有父ld值,没有先初始化存进去为空数组,之后再空数组push添加新内容完成赋值
const itemResultList = {}for (let i = 0; i < this.select.options.field.length; i++) {if (this.select.options.field[i].parentType === 'subform') {// 判断父类是否存在if (itemResultList[this.select.options.field[i].parentColumnName]) {itemResultList[this.select.options.field[i].parentColumnName].push(this.select.options.field[i].columnName)} else {itemResultList[this.select.options.field[i].parentColumnName] = []itemResultList[this.select.options.field[i].parentColumnName].push(this.select.options.field[i].columnName)}} else {array.push(this.select.options.field[i].atrName)}}
另外一种写法
for (const item of this.select.options.field) {if (item.parentType && item.parentType === 'subform') {const parentColumnName = item.parentColumnNameconst indexA = dataList.findIndex(x => x.formId === item.formId)if (dataList[indexA].itemResultList[parentColumnName]) {dataList[indexA].itemResultList[parentColumnName].push(item.atrName)} else {dataList[indexA].itemResultList[parentColumnName] = []dataList[indexA].itemResultList[parentColumnName].push(item.atrName)}} else {// 查询那个表const indexA = dataList.findIndex(x => x.formId === item.formId)// 索引存在且索引下的包含这个表单id名if (indexA !== -1 && !dataList[indexA].resultList.includes(item.atrName)) {dataList[indexA].resultList.push(item.atrName)}}}
2.普通字段和子表单数据分类处理
- 原始接口数据
[{"formId": 5768,"itemType": 2,"showName": "普通数字","subformUuid": null,"colWidth": null,"dataTypeId": 3,"type": "countNumber","atrName": "f5768c116","formName": "综合组件","options": {"mustFill": false,"readOnly": false,"hide": false,"defaultValueShow": false,"int": false,"rangeIndex": false,"min": 0,"max": 0,"setFloat": 2,"controlsPosition": "right","describe": "","dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638870650000_2482"},"id": "5768_2","key": "1638870650000_2482","columnName": "f5768c116"},{"atrName": "subf761c13","columnName": "subf761c13","formId": 5768,"dataTypeId": 3,"options": {"mustFill": false,"readOnly": false,"hide": false,"defaultValueShow": false,"int": false,"rangeIndex": false,"min": 0,"max": 0,"setFloat": 2,"controlsPosition": "right","describe": "","dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638522586000_18918"},"showName": "子表单第二个-数字","parentName": "子表单第二个","parentType": "subform","parentColumnName": "f5768c102","type": "countNumber"},{"atrName": "subf642c26","columnName": "subf642c26","formId": 5768,"dataTypeId": 1,"options": {"width": "100","format": "text","dataType": "string","describe": "","scan": false,"mustFill": false,"readOnly": false,"hide": false,"NotAllowedRepeat": false,"placeholder": "","defaultValue": "","previewValue": "","maxlength": "","placeholderShow": false,"defaultValueShow": false,"maxLengthShow": false,"dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1637143238000_70054"},"showName": "子表单第一个-单行文本","parentName": "子表单第一个","parentType": "subform","parentColumnName": "f5768c21","type": "input"},{"atrName": "subf642c27","columnName": "subf642c27","formId": 5768,"dataTypeId": 2,"options": {"width": "100","defaultValue": "","previewValue": "","pattern": "","placeholder": "","describe": "","maxlength": "","mustFill": false,"readOnly": false,"hide": false,"placeholderShow": false,"defaultValueShow": false,"maxLengthShow": false,"dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638754212000_56027"},"showName": "子表单第一个-多行文本","parentName": "子表单第一个","parentType": "subform","parentColumnName": "f5768c21","type": "textarea"},{"atrName": "subf761c10","columnName": "subf761c10","formId": 5768,"dataTypeId": 1,"options": {"width": "100","format": "text","dataType": "string","describe": "","scan": false,"mustFill": false,"readOnly": false,"hide": false,"NotAllowedRepeat": false,"placeholder": "","defaultValue": "","previewValue": "","maxlength": "","placeholderShow": false,"defaultValueShow": false,"maxLengthShow": false,"dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638522502000_72596"},"showName": "子表单第二个-单行文本","parentName": "子表单第二个","parentType": "subform","parentColumnName": "f5768c102","type": "input"}]
修改后的接口数据
[{"formId": 5768,"itemType": 2,"showName": "普通数字","subformUuid": null,"colWidth": null,"dataTypeId": 3,"type": "countNumber","atrName": "f5768c116","formName": "综合组件","options": {"mustFill": false,"readOnly": false,"hide": false,"defaultValueShow": false,"int": false,"rangeIndex": false,"min": 0,"max": 0,"setFloat": 2,"controlsPosition": "right","describe": "","dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638870650000_2482"},"id": "5768_2","key": "1638870650000_2482","columnName": "f5768c116"},{"parentColumnName": "f5768c102","children": [{"atrName": "subf761c13","columnName": "subf761c13","formId": 5768,"dataTypeId": 3,"options": {"mustFill": false,"readOnly": false,"hide": false,"defaultValueShow": false,"int": false,"rangeIndex": false,"min": 0,"max": 0,"setFloat": 2,"controlsPosition": "right","describe": "","dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638522586000_18918"},"showName": "子表单第二个-数字","parentName": "子表单第二个","parentType": "subform","parentColumnName": "f5768c102","type": "countNumber"},{"atrName": "subf761c10","columnName": "subf761c10","formId": 5768,"dataTypeId": 1,"options": {"width": "100","format": "text","dataType": "string","describe": "","scan": false,"mustFill": false,"readOnly": false,"hide": false,"NotAllowedRepeat": false,"placeholder": "","defaultValue": "","previewValue": "","maxlength": "","placeholderShow": false,"defaultValueShow": false,"maxLengthShow": false,"dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638522502000_72596"},"showName": "子表单第二个-单行文本","parentName": "子表单第二个","parentType": "subform","parentColumnName": "f5768c102","type": "input"}]},{"parentColumnName": "f5768c21","children": [{"atrName": "subf642c26","columnName": "subf642c26","formId": 5768,"dataTypeId": 1,"options": {"width": "100","format": "text","dataType": "string","describe": "","scan": false,"mustFill": false,"readOnly": false,"hide": false,"NotAllowedRepeat": false,"placeholder": "","defaultValue": "","previewValue": "","maxlength": "","placeholderShow": false,"defaultValueShow": false,"maxLengthShow": false,"dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1637143238000_70054"},"showName": "子表单第一个-单行文本","parentName": "子表单第一个","parentType": "subform","parentColumnName": "f5768c21","type": "input"},{"atrName": "subf642c27","columnName": "subf642c27","formId": 5768,"dataTypeId": 2,"options": {"width": "100","defaultValue": "","previewValue": "","pattern": "","placeholder": "","describe": "","maxlength": "","mustFill": false,"readOnly": false,"hide": false,"placeholderShow": false,"defaultValueShow": false,"maxLengthShow": false,"dataLinkFlag": false,"dataLinkage": {"dialogFlag": false,"currentFormList": [],"currentFormItem": "","otherFormId": "","otherFormList": [],"otherFormListShow": [],"otherFormCurrentItem": "","otherFormDataLinkageItem": ""},"remoteFunc": "func_1638754212000_56027"},"showName": "子表单第一个-多行文本","parentName": "子表单第一个","parentType": "subform","parentColumnName": "f5768c21","type": "textarea"}]}]
代码实现
const subData = []options.field.forEach((item, index) => {// 判断父类子表单id存在吗?if (item.parentColumnName && item.parentType === 'subform') {// 判断是否存在,存在则返回数组对象const arr = subData.filter(item2 => {return item2.parentColumnName && item2.parentColumnName === item.parentColumnName})// 若空数组先初始化if (arr.length === 0) {const obj = {parentType: item.parentType,parentName: item.parentName,parentColumnName: item.parentColumnName,children: []}obj.children.push(item)subData.push(obj)} else {arr[0].children.push(item)}} else {// 普通添加subData.push(item)}this.comOptionField = subData})
