简体   繁体   中英

Convert String to an Array in JS

In my project I have a use case like the below:

I have a response Array like below,

(4) [{…}, {…}, {…}, {…}]
0:{header: 0, name: "Name", field: "Id"}
1:{header: 3, name: "LastName", field: "Agreement__c"}
2:{header: 3, name: "LastName", field: "Amount__c"}
3:{header: 3, name: "LastName", field: "BIC__c"}
length:4

from the above I convert the above array to String by using,

JSON.stringify(responseArray) and store it in a string field.

After that I want to do some manipulation dynamically to that value of that field. So when I get the value back from the field it came as like below,

[{"header":0,"name":"Name","field":"Id"}, 
{"header":3,"name":"LastName","field":"Agreement__c"}, 
{"header":3,"name":"LastName","field":"Amount__c"}, 
{"header":3,"name":"LastName","field":"BIC__c"}]

Anyone please help me to convert the above string response to an Array in Javascript like as follows,

index 0 -> {"header":0,"name":"Name","field":"Id"}
index 1 -> {"header":3,"name":"LastName","field":"Agreement"}

I have tried with the split function but couldn't able to achieve the exact need.

Put square brackets at the beginning and end of your string and call JSON.parse :

 $ node

 > const text = `{"header":0,"name":"Name","field":"Id"}, 
    {"header":3,"name":"LastName","field":"Agreement"}, 
    {"header":3,"name":"LastName","field":"Amount"}, 
    {"header":3,"name":"LastName","field":"BIC"}`

 > JSON.parse(`[${text}]`)
 [ { header: 0, name: 'Name', field: 'Id' },
   { header: 3, name: 'LastName', field: 'Agreement' },
   { header: 3, name: 'LastName', field: 'Amount' },
   { header: 3, name: 'LastName', field: 'BIC' } ]

you can use following code sample first append "[" at begging of your string and "]" at end of your string so your string will be well formatted as JSON array then it is so easy to parse it using JSON.parse built in function

 a = '['+'{"header":0,"name":"Name","field":"Id"}, {"header":3,"name":"LastName","field":"Agreement"}, {"header":3,"name":"LastName","field":"Amount"}, {"header":3,"name":"LastName","field":"BIC"}'+"]"

 var myarray = JSON.parse(a);

是的, JSON.parse是真正简单的答案。

You just need some basic string manipulations, By the way, I changed your string into a valid syntax

 var str="{\\"header\\":0,\\"name\\":\\"Name\\",\\"field\\":\\"Id\\"},{\\"header\\":3,\\"name\\":\\"LastName\\",\\"field\\":\\"Agreement\\"},{\\"header\\":3,\\"name\\":\\"LastName\\",\\"field\\":\\"Amount\\"},{\\"header\\":3,\\"name\\":\\"LastName\\",\\"field\\":\\"BIC\\"}"; str=str.replace(/},{/g,"}|{"); var arr = str.split("|"); var json = []; for(i=0; i<arr.length; i++){ json.push(JSON.parse(arr[i])); } //console.log(json); console.log(json[0]); console.log(json[1]); 

try this

  var textstr = '[{"header":0,"name":"Name","field":"Id"},{"header":3,"name":"LastName","field":"Agreement"}, {"header":3,"name":"LastName","field":"Amount"}, {"header":3,"name":"LastName","field":"BIC"}]'; var textstr2 = JSON.parse(textstr); console.log(textstr2) 

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