Конвертировать рекурсивный объект с двумя типами дочерних к типу с одним
С сервера приходит json, в котором есть два варианта дочерних элементов
Folder(title : string, id: string, childfolders?: Folder[], childTemps?: Template[], parentFolderId?: string)(ключевой и всегда корневой)Template(id: string, title: string, value: string, folderId: string, isDeleted: boolean)
Надо конвертировать в TreeData(id: string, title: string, children: TreeData) (P.S. могут быть еще параметры, что заполняются вручную)
Как можно решить такую задачу?
Ответы (1 шт):
Автор решения: qwabra
→ Ссылка
interface Template {
id: string,
title: string,
value: string,
folderId: string,
isDeleted: boolean
}
interface Folder {
id: string,
title: string,
childfolders?: Folder[],
childTemps?: Template[],
parentFolderId?: string
}
interface TreeData {
id: string,
title: string,
children: TreeData | undefined
}
type $convert = (f: Folder) => TreeData
const convert: $convert = f => {
let res: TreeData, children: TreeData['children']
const { id, title, childfolders } = f
if (undefined !== childfolders) {
// FIXME: childfolders[0]
children = convert(childfolders[0])
}
res = {
id,
title,
children,
}
return res
}