编辑代码

//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
// console.log("Hello JSRUN!   \n\n         - from NodeJS .");

const response = {
    "type": "object",
    "properties": {
        "success": {
            "type": "boolean",
            "extensions": {
                "x-java-type": "java.lang.Boolean"
            }
        },
        "errorCode": {
            "type": "string",
            "extensions": {
                "x-java-type": "java.lang.String"
            }
        },
        "errorMessage": {
            "type": "string",
            "extensions": {
                "x-java-type": "java.lang.String"
            }
        },
        "traceId": {
            "type": "string",
            "extensions": {
                "x-java-type": "java.lang.String"
            }
        },
        "data": {
            "type": "array",
            "items": {
                "title": "MonitorTaskVO",
                "description": "",
                "type": "object",
                "properties": {
                    "page": {
                        "type": "integer",
                        "description": "当前页,可选,默认=1",
                        "format": "int32",
                        "extensions": {
                            "x-java-type": "java.lang.Integer"
                        }
                    },
                    "pageSize": {
                        "type": "integer",
                        "description": "每页数量,可选,默认=10",
                        "format": "int32",
                        "extensions": {
                            "x-java-type": "java.lang.Integer"
                        }
                    },
                    "limit": {
                        "type": "integer",
                        "description": "每次查询数量,等同于pageSize。仅供查询",
                        "format": "int32",
                        "extensions": {
                            "x-java-type": "java.lang.Integer"
                        }
                    },
                    "offset": {
                        "type": "integer",
                        "description": "偏移量,仅供查询",
                        "format": "int32",
                        "extensions": {
                            "x-java-type": "java.lang.Integer"
                        }
                    },
                    "id": {
                        "type": "integer",
                        "description": "主键",
                        "format": "int64",
                        "extensions": {
                            "x-java-type": "java.lang.Long"
                        }
                    },
                    "gmtCreate": {
                        "type": "string",
                        "description": "创建时间",
                        "format": "date",
                        "extensions": {
                            "x-java-type": "java.util.Date"
                        }
                    },
                    "gmtModified": {
                        "type": "string",
                        "description": "修改时间",
                        "format": "date",
                        "extensions": {
                            "x-java-type": "java.util.Date"
                        }
                    },
                    "taskId": {
                        "type": "string",
                        "description": "任务ID",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "templateId": {
                        "type": "string",
                        "description": "模版ID",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "fileName": {
                        "type": "string",
                        "description": "文件名称",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "filePath": {
                        "type": "string",
                        "description": "文件路径",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "fileSize": {
                        "type": "string",
                        "description": "文件名称",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "fileType": {
                        "type": "string",
                        "description": "文件类型",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "status": {
                        "type": "string",
                        "description": "执行状态",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "details": {
                        "type": "string",
                        "description": "详情说明",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "appId": {
                        "type": "string",
                        "description": "appID",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "instId": {
                        "type": "string",
                        "description": "机构ID",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    },
                    "deadline": {
                        "type": "string",
                        "description": "文件传输截止时间",
                        "extensions": {
                            "x-java-type": "java.lang.String"
                        }
                    }
                },
                "extensions": {
                    "x-java-type": "com.mycm.file.mycmfilemanage.core.service.model.vo.MonitorTaskVO",
                    "x-parent-class": "com.mycm.file.mycmfilemanage.core.service.domain.PageDTO"
                }
            },
            "extensions": {
                "x-generic-index": 0,
                "x-java-type": "E",
                "x-generic-type-var": true
            }
        }
    }
};

// function clearSchema(
//     schema,
//     items,
//     keys,
// ) {
//     const itemFields = items || ['properties', 'items']
//     const keyFields = keys || ['type', ...itemFields];
//     return Object.keys(schema).reduce((pre, cur) => keyFields.includes(cur) ? {
//         ...pre,
//         [cur]: itemFields.includes(cur)
//             ? Object.keys(schema[cur]).reduce((itemPre, itemCur) => ({
//                 ...itemPre,
//                 [itemCur]: clearSchema(schema[cur][itemCur], itemFields, keyFields)
//             }), Object.create(null)) : schema[cur]
//     } : pre, Object.create(null));
// }


// console.time();
// console.log(clearSchema(response))
// console.timeEnd();

function clear(
    schema,
    keys,
) {
    const propertiesKey = 'properties';
    const itemsKey = 'items';
    const keyFields = keys || ['type'];
    return Object.keys(schema).reduce((pre, cur) => {
        if (keyFields.includes(cur)) {
            // 保留属性
            return {
                ...pre,
                [cur]: schema[cur]
            };
        } else if (cur === itemsKey) {
            // 数组
            const value = schema[itemsKey][propertiesKey];
            // console.log('1', value);
            const childrenProperties = Object.keys(value).reduce((itemPre, itemCur) => ({
                ...itemPre,
                [itemCur]: clear(value[itemCur], keyFields)
            }), Object.create(null));
            return {
                ...pre,
                items: {
                    properties: childrenProperties
                }
            }
        } else if (cur === propertiesKey) {
            // 对象
            const value = schema[propertiesKey];
            // console.log('2', value);
            const childrenProperties = Object.keys(value).reduce((itemPre, itemCur) => ({
                ...itemPre,
                [itemCur]: clear(value[itemCur], keyFields)
            }), Object.create(null));
            return {
                ...pre,
                properties: childrenProperties
            }
        } else {
            return pre;
        }
    }, Object.create(null));
}

console.time();
console.log(clear(response));
console.timeEnd();