Skip to content
Advertisement

TypeScript enum to specific object

I have the following TypeScript enum:

export declare enum SupportedLanguages {
    en,
    fr
}

If I import it in my react application and console.log it, I will get the following object returned:

{
  en: "en", 
  fr: "fr"
}

How can I manipulate it, so that I get the following object returned?

{
  en: "",
  fr: ""
}

I tried it with const Lang = Object.keys(SupportedLanguages) and also with .map() but I did not get the expected object returned.

Advertisement

Answer

Are you just looking to get a new object with all the data as empty strings?

var supportedLanguages = {
  en: "en", 
  fr: "fr"
};

var result = Object.keys(supportedLanguages)
    .reduce((accum, key) => 
        Object.assign(accum, { [key]: "" })
    , {});

console.log(result); // { "en": "", "fr": "" }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement