1

I want to convert a typescipt object to JSON

ObjectToConvert = { key : value }

public getJson(key: string, value: string) {
    //I passed this values when I called the method : key="attr",value="on"
   console.log(JSON.stringify({ ObjectToConvert }))
   // The result is {"key":"on"} instead of {"attr":"on"}
   

I want to know why the result is not what I passed as parameter.

1 Answer 1

1

Use computed property names:

ObjectToConvert = { [key] : value }
Sign up to request clarification or add additional context in comments.

1 Comment

it works, thank you for helping :)

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.