lodash enum to array Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Mon, 11 Sep 2023 11:18:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 lodash enum to array Archives - Tech Insights 32 32 230003556 TypeScript Enum | How to Convert Enum into an Array in Typescript https://reactconf.org/typescript-enum-how-to-convert-enum-into-an-array-in-typescript/ https://reactconf.org/typescript-enum-how-to-convert-enum-into-an-array-in-typescript/#respond Mon, 11 Sep 2023 11:18:13 +0000 https://labpys.com/?p=1618 Enums are a set of named constants in TypeScript. Enums are useful when you have a fixed set of constants. Sometimes we need to  Convert an enum into an array …

The post TypeScript Enum | How to Convert Enum into an Array in Typescript appeared first on Tech Insights.

]]>
Enums are a set of named constants in TypeScript. Enums are useful when you have a fixed set of constants. Sometimes we need to  Convert an enum into an array in Typescript, and this can be quite easy.

In this article, we will learn How to Convert an enum into an array in Typescript with some examples.

Let’s get started,

Enums As we have discussed, define a set of named constants that exist at runtime as real objects.

TypeScript Enum

Convert numeric enum into an array

To convert a numeric enum into an array we need to use the “Object.values” with the filter function

Let’s have an example

enum gender{
    Male,
    Female,
    Others
}
console.log(Object.values(gender));

When we run this example we will get this result.

> tsc enumtoarray.ts 
> node enumtoarray.js

[ 'Male', 'Female', 'Others', 0, 1, 2 ]

Suppose if we need to filter out only the numeric value then we have to use the filter function.

Like so,

enum gender{
    Male,
    Female,
    Others
}
console.log(Object.values(gender).filter(val=>isNaN(Number(val))));
 

If we need to filter out only the string value.

enum gender{
    Male,
    Female,
    Others
}

console.log(Object.values(gender).filter(val=>!isNaN(Number(val))));

Here is the result:

[ 'Male', 'Female', 'Others' ]
[ 0, 1, 2 ]

Convert a string enum into an array

To convert a string enum into an array we have to use the Object.values or  Object.key functions

Let’s have an example

enum department{
    CS = 'Computer Science Engg',
    Mech = 'Mechanical Engineering',
    EC = 'Electronic and Communication Engg',
    CV = 'Civil Engineering'
}

console.log(Object.keys(department));
console.log(Object.values(department));

Here is the output:

[ 'CS', 'Mech', 'EC', 'CV' ]
[
  'Computer Science Engg',
  'Mechanical Engineering',
  'Electronic and Communication Engg',
  'Civil Engineering'
]

Converting an Enums Key-value pair into an array

To transform an enum into an array of key-value pairs, we need to use the Object.entries() Method with applying a filter function to remove any pairs where the value is not a number.

const Gendernum : [string,number][]=Object.entries(gender)
.filter(([key,value])=> typeof value === 'number')
.map(([key,value])=> [key,value as number]);

console.log(Gendernum);

Result:

[ [ 'Male', 0 ], [ 'Female', 1 ], [ 'Others', 2 ] ]

enum department{
    CS = 'Computer Science Engg',
    Mech = 'Mechanical Engineering',
    EC = 'Electronic and Communication Engg',
    CV = 'Civil Engineering'
}
export const gendermodel:{
    value: string;
    key : string;
}[]=Object.entries(department)
.map(([key,value])=>({key,value}));

console.log(gendermodel);

Result:

[
  { key: 'CS', value: 'Computer Science Engg' },
  { key: 'Mech', value: 'Mechanical Engineering' },
  { key: 'EC', value: 'Electronic and Communication Engg' },
  { key: 'CV', value: 'Civil Engineering' }
]

More Articles – How to Create a Multi-Select Dropdown List in Angular

The post TypeScript Enum | How to Convert Enum into an Array in Typescript appeared first on Tech Insights.

]]>
https://reactconf.org/typescript-enum-how-to-convert-enum-into-an-array-in-typescript/feed/ 0 1618