Convert an array of strings to my custom array type in Angular

I have a list of different statuses

Here is an example of it:

 export enum InvoiceStatus {
    Created = 1,
    Pending = 2,
    Approved = 3,
    Rejected = 4,
    Paid = 5,
    Deleted = 6,
    PreparingPayment = 7
}

My goal is to convert it into an array of type SelectItem

    export interface SelectItem {
    label?: string;
    value: any;
    styleClass?: string;
    icon?: string;
    title?: string;
    disabled?: boolean;
}

I want to assign the name of the status as the label and its corresponding value

Any suggestions on how I can achieve this?

Answer №1

function CheckIfStringIsNumber(input) {
    return !isNaN(Number(input)) === true;
}

// Convert enum to array
function EnumToArray(enumValue) {
    return Object.keys(enumValue)
        .filter(CheckIfStringIsNumber)
        .map(property =>  ({'name': enumValue[property], 'id': property}));
}
let status:any=InvoiceStatus;

let options:SelectItem[]

options=EnumToArray(status)

Answer №2

Give this a try

 let objectList: SelectItem[] =[];
      
for (const [key, value] of Object.entries(InvoiceStatus)) {  
      if (!Number.isNaN(Number(key))) {  
        continue;  
    }  
    objectList.push({ value: value, label: key });  
}  
  
console.log(objectList);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Does an event fire after the onclick event completes?

After an onclick event, I need a particular code to be executed. This works well on mobile devices using touchstart and touchend events. However, is there an equivalent event for computers? This is how my current code looks like: document.getElementById( ...

All file upload requests consistently result in a status code of 400

I am encountering an issue with file uploading in Express. Every time I send a request with multipart/form-data, I receive a 400 bad request response with no error message, just an empty object. Currently, I am using busboy-body-parser for parsing multipar ...

Transitioning from version 3 to version 4 has resulted in an issue where tooltips are not displaying properly

After following a tutorial on creating a scatter plot using this reference, everything worked perfectly in version 3. However, upon switching to version 4, I encountered issues with the tooltip, x-axis, and y-axis not appearing. To troubleshoot, I created ...

Sending information from an AngularJS selected item to an edit form

Currently, I am working on an add/edit form using angularJS. Within this project, there are two templates - one for displaying a list of items and another for the form itself. While adding new items works smoothly, I have encountered some challenges when i ...

Make a div with absolute positioning overflow outside of a div with relative positioning that is scrollable

I am facing an issue with two columns positioned side by side. The right column contains a tooltip that overflows to the left on hover. Both columns are scrollable and relatively positioned to allow the tooltip to follow the scroll. However, the tooltip is ...

Executing a C# method from within a .js file using a Javascript function in a .cs file

I've made some updates based on the responses provided. My main area of confusion lies in the 'data' parameter and how to handle it in the JavaScript call. C# method [HttpPost] public string GetPreviewURL(string activityID) ...

{Multi Drag-and-drop} Enhancing the User Experience: Implementing Individual Comboboxes for Each File Prior to Up

Utilizing the dropzone tool for uploading files to my server has been quite helpful. I am able to drag and drop multiple files, sending them successfully with a post request. For each uploaded file, I require specific information related to it. It would b ...

Convert Binary Data to PDF Using Javascript Through Streaming

Upon requesting a Web-service, I received the following response (PDF file Streamed) %PDF-1.5 %µµµµ 1 0 obj <</Type/Catalog/Pages 2 0 R/Lang(en-GB) /StructTreeRoot 10 0 R/MarkInfo<</Marked true>>>> endobj 2 0 obj <</Type/ ...

What are some solutions for addressing the viewport width problem that comes up when incorporating Google+ comments on a responsive website

My website looks great on both desktop and mobile devices without any viewport issues, until I added Google+ comments. These comments will automatically set the width of the page to a specified amount: <!-- Google comments --> <script src="https: ...

Error: You forgot to include a name after the dot operator

I am facing an issue where I am unable to use YUIcompressor to compress a file. The script is running smoothly, but I am encountering the error missing name after . operator on line 3 of this script: Specifically at: "+ source.response.chars[k].name +" I ...

TS2786 TypeScript is failing to recognize the UI-Kitten components

Error message on IDE: Error Encountered: 'ApplicationProvider' cannot be used as a JSX component. The instance type 'ApplicationProvider' is not a valid JSX element. The types returned by 'render()' are incompatible betwe ...

The custom attribute in jQuery does not seem to be functioning properly when used with the

I am currently working with a select type that includes custom attributes in the option tags. While I am able to retrieve the value, I am experiencing difficulty accessing the value of the custom attribute. Check out this Jsfiddle for reference: JSFIDDLE ...

When attempting to use dynamic imports with `react-icons`, NextJS will import all necessary components and dependencies

My current task involves incorporating an Icon from the react-icons package into my project. However, when I attempt to do so using an import statement, the resulting bundle size looks like this: Route (pages) Size First Lo ...

Tips for effectively utilizing foreach loops in JQuery

I've encountered an issue while displaying a list of data in a table using Smarty templates and foreach loops. I'm trying to make each line clickable to show new data below it, but currently, this functionality only works for the first line. Is t ...

Change the value of a single element in an array using a component in ReactJS

I'm attempting to pass an array value to a specific index in one component and then assign a desired value from the child component. I need it to work this way because I'm developing a survey app where the number of questions can vary. This is j ...

Start the Vue component only after ensuring that the element has been fully loaded

I have created a small vue.js component in JavaScript. Is there an elegant way to instantiate the vue component only when the element is loaded? The problem I am facing is that I'm receiving warnings in other HTML files where the element does not ex ...

Adjust the color of the entire modal

I'm working with a react native modal and encountering an issue where the backgroundColor I apply is only showing at the top of the modal. How can I ensure that the color fills the entire modal view? Any suggestions on how to fix this problem and mak ...

Using Meteor package to efficiently import JSON arrays into mongoDB

I am currently working on developing a meteor package that will allow users to import JSON files into collections in a mongoDB. However, I'm uncertain about the feasibility of this task. The idea is for the user to upload a JSON file and specify the ...

Guide: Looping through a typed Viewbag array - best practices

I have passed a List<AdminUsers> through the Viewbag to a view. This list is then assigned to a JavaScript variable and looped through. However, when debugging on the view, I noticed that the for loop is not being executed, even though I set a break ...

What is the best method for extracting attribute values from multiple child elements using puppeteer?

When using this code, I can retrieve the attribute value of the first element selected. By adding /html/body/section/div[3]/img<2> or img<3> in the xpath, I am able to retrieve data for subsequent img elements. However, the parent element on t ...