Vaibhav D
Vaibhav D

Reputation: 625

Angular: Change the class member names dynamically

In the angular ts file, I have a const like below

const baseMaps = {
            Map: "test 1",
            Satellite: "test 2"
    };

And I want to set Map and Satellite member names dynamically. Below will not work but I need a solution something like below.

const mapText = "Map";
const baseMaps = {
            mapText : "test 1",
            Satellite: "test 2"
    };

Can anyone guide me on how to do this?

Upvotes: 0

Views: 31

Answers (1)

Kanishk Anand
Kanishk Anand

Reputation: 1702

Using the [...] you can dynamically infer the key of an object.

You can do the following:

const mapText = "Map";
const satelliteText = "Satellite";
const baseMaps = {
           [mapText] : "test 1",
           [satelliteText]: "test 2"
    };

Upvotes: 1

Related Questions