Home > Blockchain >  Object.keys brings index values of enum
Object.keys brings index values of enum

Time:12-20

I have an enum

export enum IAgentTask {
    firstName,
    lastName,
    idNumber,
    srId,
    srCreationDate,
    selected,
    phoneNumber,
    offerId,
    clientId,
    teamName,
    userFirstName,
    userLastName,
    agentName,
    mainreferrerName
   ....
 }

I need to get only the key (list of all the options as string) but using Object.keys(IAgentTask) bring a list with also the number of the property(26) like this: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', 'firstName', 'lastName'].....why? also after using the list to compare the properties of another list- I want to remove the property of response.tasks that are not in test how can I do it dynamic?

  let test = this.filteredRowsPreference.map(x => x.fieldDbKey);
              for (let j = 0; j < response.tasks.length; j  ) {
                let f= Object.keys(IAgentTask);
                  for (let key in Object.keys(IAgentTask))
                      if (!test.includes(key))
                          delete response.tasks[j][key]

              }

response.tasks[j][key] is undefined and I don't want to acsses every propery like this: response.tasks[j].firstName

CodePudding user response:

Enums in TS can be accessed both by key and value. When you declare an enum and don't assign values to the keys, they automatically follow a numbered sequence.

If all you need is getting the list of items in the enum, a quick hack would be filtering out things that don't parse to numbers so you omit the implicit values of the enum:

Object.keys(IAgentTask).filter((key) => Number.isNaN(parseInt(key, 10)))

CodePudding user response:

The easiest way to address this is to use string enums, i.e.

export enum IStringAgentTask {
    firstName = 'firstName',
    lastName = 'lastName',
    idNumber = 'idNumber',
    srId = 'srId',
    srCreationDate = 'srCreationDate',
    selected = 'selected',
    phoneNumber = 'phoneNumber',
    offerId = 'offerId',
    clientId = 'clientId',
    teamName = 'teamName',
    userFirstName = 'userFirstName',
    userLastName = 'userLastName',
    agentName = 'agentName',
    mainreferrerName = 'mainreferrerName',
}

Object.keys() will then yield the following:

console.log(Object.keys(IStringAgentTask))
// ["firstName", "lastName", "idNumber", "srId", "srCreationDate", "selected", "phoneNumber", "offerId", "clientId", "teamName", "userFirstName", "userLastName", "agentName", "mainreferrerName"]

However if you need to use the basic enum (i.e. you prefer/benefit from the numeric value of the enum), you can count on the fact that the array that is produced is twice the length of the number of enum values you have listed, as it contains all of the values (numbers) listed sequentially followed by all of the keys (the enum properties). You can therefore just discard the first half of the array and be left with the same array as in the previous example:

export enum IAgentTask {
    firstName,
    lastName,
    idNumber,
    srId,
    srCreationDate,
    selected,
    phoneNumber,
    offerId,
    clientId,
    teamName,
    userFirstName,
    userLastName,
    agentName,
    mainreferrerName,
}

function getEnumValues(): string[] {
    const originalEnumValues: (string | number)[] = Object.keys(IAgentTask);
    const halfOriginalEnumValuesLength = originalEnumValues.length / 2
    return originalEnumValues.splice(halfOriginalEnumValuesLength, halfOriginalEnumValuesLength) as string[]
}

console.log(getEnumValues());
// ["firstName", "lastName", "idNumber", "srId", "srCreationDate", "selected", "phoneNumber", "offerId", "clientId", "teamName", "userFirstName", "userLastName", "agentName", "mainreferrerName"]

TypeScript Playground

  • Related