const transformObject = (obj) => {
let result = {};
for (let key in obj) {
for (let lang in obj[key]) {
if (!result[lang]) {
result[lang] = {};
}
result[lang][key] = obj[key][lang];
}
}
return result;
};
{
overview: {
ru: 'Общая информация',
en: 'Overview',
},
configuration: {
ru: 'Конфигурация',
en: 'Configuration',
}
}
ru: {
overview: 'Общая информация',
configuration: 'Конфигурация'
},
en: {
overview: 'Overview',
configuration: 'Configuration',
}
type TransformType<T> = {
[K in keyof T[keyof T]]: {
[K1 in keyof T]: T[K1][K];
}
}
type Rec = Record<string, Record<string, string>>;
function transformObject<T extends Rec>(obj: T): TransformType<T> {
let result = {} as Rec;
Object.keys(obj).forEach((k) => {
Object.keys(obj[k]).forEach((k1) => {
if (!result[k1]) {
result[k1] = {};
}
result[k1][k] = obj[k][k1];
});
})
return result as TransformType<T>;
};
const src = {
overview: {
ru: 'Общая информация',
en: 'Overview',
},
configuration: {
ru: 'Конфигурация',
en: 'Configuration',
}
};
const res = transformObject(src);
const x = res.en; // { overview: string; configuration: string; }
const transformObject = <const K0 extends string, const K1 extends string>(obj: Record<K0, Record<K1, string>>) => {
let result: Partial<Record<K1, Partial<Record<K0, string>>>> = {};
for (const key of Object.keys(obj) as K0[]) {
for (const lang of Object.keys(obj[key]) as K1[]) {
(result[lang] ??= {} as Partial<Record<K0, string>>)[key] = obj[key][lang];
}
}
return result;
};