简体   繁体   中英

convert TypeScript object to JSON

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.

Use computed property names :

ObjectToConvert = { [key] : value }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM