// 我提供解决方案如下:我提供完整通用性的实现的方案,你可以先对老师的答案思考下,留一个思考的空间给你!
const hasOwnProperty = Object.prototype.hasOwnProperty
export const hasOwn = (
val: object,
key: string | symbol
): key is keyof typeof val => hasOwnProperty.call(val, key)
const GetSlaParams = <T extends object>(data_: T) => {
let copyData = {}
for (let key in data_) {
if (hasOwn(data_, key)) {
console.log(key)
copyData[key] = data_[key]
}
}
return Object.keys(copyData).map((key) => {
if (hasOwn(copyData, key)) {
return formatSlectObjList(copyData[key])
}
return {}
})
}
const formatSlectObjList = <T>(arr: T[]): { label: T, value: T }[] => {
const value = arr.map((item: T) => {
return { label: item, value: item }
})
return value
}
let result = GetSlaParams(data)
console.log("result:", result)