Reputation: 625
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
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