3

I have a constant class in Typescript and it has the followings.

export class Constants {
    public static readonly SERVER_1: string = "server url 1";
    public static readonly SERVER_2: string = "server url 2";
    public static readonly SERVER_3: string = "server url 3";
    .....
    public static readonly SERVER_21: string = "server url 21";
}

In my Processor class, I need to form the object and I push to an array like this for processing.

export class Processor {

    public areReachable(): void {
        const myObject1: ServerReachObject1 = new ServerReachObject(Constants.SERVER_1);
        const myObject2: ServerReachObject2 = new ServerReachObject(Constants.SERVER_2);
        const myObject3: ServerReachObject3 = new ServerReachObject(Constants.SERVER_3);
        .....
        const myObject21: ServerReachObject21 = new ServerReachObject(Constants.SERVER_21);
        const sModelArray: DNSServerModelInfo[] = [];
        ftpModelServerArray.push(myObject1, myObject2, myObject3 ..... myObject21);
    }

}

Is there any better way to write the above. In future, the servers may be added. My senior team members are suggesting me to write in a better way. Please suggest me and help me.

I tried to write like this.

for (let i = 1; i < 22; i++) {
    const dynamicServerName = Constants + ".SERVER_"+i;
    const myObject1: ServerReachObject = new ServerReachObject(dynamicServerName);
    const sModelArray: DNSServerModelInfo[] = [];
    ftpModelServerArray.push(sModelArray);
}

But it is not working. My objective is to form the string dynamically from the Constant class instead of declaring manually one by one. Please help me.

6
  • 5
    const dynamicServerName = Constants[`SERVER_${i}`] Commented Sep 30, 2020 at 7:39
  • Sir, I will check now. Commented Sep 30, 2020 at 7:39
  • Does this answer your question? Dynamically access object property using variable Commented Sep 30, 2020 at 7:43
  • I would suggest asking your senior members for a better approach first as they may have something particular in mind + they know the structure maybe a bit better. Your approach is a good attempt, but imagine someone adds another server - he would need to adjust i everytime one is added. You're also recreating the array in every loop cycle - this is not going to work Commented Sep 30, 2020 at 7:45
  • Jaromanda Sir, it worked. Excellent Sir. I really learnt. Commented Sep 30, 2020 at 7:49

1 Answer 1

1

Please consider using a const object instead of a class with const fields. You can then iterate the server url values with Object.values().

type ServerConstants = {
    readonly [name: string]: string;
}

export const Server: ServerConstants = {
    SERVER_1: 'Server url 1',
    SERVER_2: 'Server url 2'
}

Server.SERVER_1 = 'foo'; // throws error correctly because of readonly in ServerConstants type

Object.values(Server).forEach(value => console.log(value));
Sign up to request clarification or add additional context in comments.

1 Comment

Mam, this answer is good but I have to make changes as suggested by you. My senior members may not accept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.