简体   繁体   中英

How to convert a String into an Object in JavaScript

I have a response coming in string format:

returnData = "1002246*9994456861#111#222*ACTIVE"

In need to modify that string data into an object :

{returnData : "1002246*9994456861#111#222*ACTIVE"}

The below is what I tried:

returnData = Object.assign({}, returnData)

But it throws me results in array for every single characters. How to convert that string variable into the object ?

Just create an object with the current value:

 returnData = "1002246*9994456861#111#222*ACTIVE" returnData = { returnData } console.log(returnData);

To create the object you want from a string, just use object literal notation:

data = {
    returnData: returnData
}

You can try to use Object.keys() method, like this:

 returnData = "1002246*9994456861#111#222*ACTIVE"; console.log({[Object.keys({returnData})[0]]: returnData});

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