0

I need to

type A {
  f1: string;
  f2; string;
}

By other side I have a variable contains the field name:

let fieldName: string = "f2";

I'd like to create an object using fieldName:

{"content of fieldName": "sdf"}

Any ideas?

2 Answers 2

2

As of ES2015 you can use computed properties.

For your example that would mean:

{ [fieldName]: "sdf" }
Sign up to request clarification or add additional context in comments.

Comments

1

Just use {[fieldName]:'sdf'}. Here is a blitz

Comments

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.