Creating an array using the variables in a class

I am seeking a way to create an array in TypeScript that contains the values of existing class variables.

For example:

export class example{
  static readonly a = 'aval';
  static readonly b = 'bval';
  static readonly c = 'cval';
}

TO

exampleArray = ['aval', 'bval', 'cval'];

Note: My goal is to gather all variable values from the class into one array, rather than accessing each variable individually like [example.a].

Answer №1

Using <strong>Object.values</strong> on the variable <strong>example</strong> should return the expected results.

Answer №2

Object.values(SomethingClass);

If you use this code snippet, it will return an array with the values of static class members. However, please note that this method will not work for non-static members.

In order to retrieve non-static members, you will need to create an instance of the class first:

Object.values(new SomethingClass());

Executing this will give you access to only the non-static members.

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

What is the correct way to test setInterval() statements within Angular?

Here is a simple code snippet I am working on: public async authenticate(username: string, password: string) { const authenticationResponse = await this.dataProvider.authenticate(username, password); if (authenticationResponse.result.code == 0) { ...

Utilizing a function within a loop that is nested within another loop

//Exercise 3a: for (var a=1; a<12; a++) { for (var b=00; b<60; b++) { for (var c=00; c<60; c++) { console.log(a + ':' + b + ':' + c + 'AM'); } } } for (var alpha=12; alpha<13; al ...

Dealing with API Troubles: Resolving 'AxiosError: Request failed with status code 401' Issue in ReactJS

I encountered the following error: AxiosError: Request failed with status code 401 While attempting to log in to my react app, I researched similar solutions and discovered that some answers suggested the issue could be related to missing headers in Axi ...

Tips for presenting dummy content in an Angular application with HTML

I have created a mock service file and I would like to display it in my HTML, but I'm not quite sure how to make it display correctly. Any help or suggestions would be greatly appreciated. <div class="container2"> <div class=" ...

Receiving no feedback from a public API while coding in React.js

I am currently delving into React.js and have been experimenting with a basic form. The issue I am facing is related to sending form data using ajax and not receiving a response. Upon running the code, the console displays an error message stating readysta ...

Are getter and setter pairs permitted to override properties in the base class of JavaScript/TypeScript?

Recently, there has been an update in TypeScript that triggers a compilation error when attempting to override a property in the base class with a getter/setter pair. This issue arises from property access augmentation: class BaseClass { prop : ...

Locate every instance of items in an array within a string

My files have a structure similar to this: https://i.stack.imgur.com/KyaVY.png When a user conducts a search, they send an array to the backend. This array always includes at least one element. For example, if they send ['javascript'] to the b ...

Loading menu items into MUI Select from a JSON file

Looking to dynamically populate an MUI select with choices retrieved from an API's JSON response. Currently, all Choices are being inserted into a single MenuItem within the Select component. Considering that the Choices may change in the future, I&a ...

Two entities positioned on opposite extremes

Is there a way to design a div with two elements (text and image) positioned on opposite sides? The length of the text varies as it's a list of months for selection. Ideally, the text should appear on the left side of the div as a "p" element, while t ...

Observing changes in a parent component variable with Angular

One feature of my form is that it consists of a parent component and a child component. To disable a button within the form, I utilize a function called isDatasetFilesValid() which checks a specific variable (datasetList[i].fileValid). This variable is mo ...

What is the reason behind only receiving one-dimensional arrays?

var $arr = { field_hide : '', field_values: '', field_triggers: {} } function setTrigger ($trigger, $values, $show, $arr) { $arr.field_hide += $show + ','; $arr.field_values += $values + ...

What differences occur in JavaScript when the IE Developers Tools are accessed?

When the IE Developers Tools are opened, what changes in terms of running an AJAX application? I am currently investigating a mysterious bug related to PrimeFaces dropdown lists in Internet Explorer. Sometimes these dropdowns do not open when clicked, but ...

Altering the src attribute of an Iframe in real-time

Local storage value needs to be passed into iframe URL using proper syntax. The code snippet below demonstrates the attempt: <script> // Get value from localstorage var x = localStorage.getItem("firstname"); // Display alert with stored val ...

Setting values and options in Material UI AutoComplete and then assigning the selected value to an object when onChange event occurs - how can it be

I am working on a project where I have a brand object with the field cat_id. When the page loads, categories are loaded into an autocomplete select. My goal now is to set the value of category id as the value for a category option in the autocomplete, an ...

Issue with AngularJS: The view is not being updated after a nested http call

There is a specific scenario where I am required to make a call to the Github API in order to fetch the information of a particular user. Following that, I need to issue another call to retrieve the repositories belonging to that user: search(login: strin ...

What are the various functions that an Alert button can serve in Ionic?

What are the potential choices for the role designation of a button within an Alert using the Ionic framework (v5)? The official documentation only lists cancel: https://ionicframework.com/docs/api/alert#buttons If desired, a button can have a role prop ...

Error: Unable to locate module: Unable to locate '@material-ui/core/Container'

Encountering an error in the browser: Error message: Module not found: Can't resolve '@material-ui/core/Container' The search for the component is directed towards my components directory instead of node_modules. Unfortunately, changing ...

Is there a way to verify that only the image was uploaded?

// why isn't this code working when I upload 1.chicken.jpg with a size of 180kb and 2.chicken.pdf, but only the chicken.pdf file gets inserted into the database? HttpFileCollection hfc = Request.Files; if (hfc != null) ...

Issues with the functionality of AngularJS routing system

angular.module('Stations', ['ngRoute']) .config(function ($routeProvider) { $routeProvider .when('/documents/:stationId', { templateUrl: '/Scripts/Modules/checklist/views/index.html', ...

Is there a way to resolve the issue of retrieving the processed value directly in NestJS's @OnEvent function?

Due to excessive logic in the API and its slow performance, I have resorted to handling some of the logic with @OnEvent. The problem arises when the frontend runs the @GET API immediately after this API, potentially without waiting for @OnEvent to update. ...