Home >Backend Development >PHP Tutorial >javascript - Front-end assembly string problem ==
There is a push string loop. How to assemble alert into data in the exact same format as below? Single quotes and commas are all needed
<code>for(var i=0;i<res.data.length;i++) { } </code>
['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white']
The front end is so bad
There is a push string loop. How to assemble alert into data in the exact same format as below? Single quotes and commas are all needed
<code>for(var i=0;i<res.data.length;i++) { } </code>
['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white']
The front end is so bad
JSON.stringify(res.data)
This is standard json with double quotesJSON.stringify(res.data).replace(/'/g, '"')
Just replace it.
You should write out the string format first!
There are many types of string processing. You send out the string to be processed. If you don’t know the method, you can take a look at Baidu
<code class="js">"['" + res.data.join("', '") + "']"</code>
<code>for(var i = 0; i < res.data.length; i++) { res.data[i] = "'" + res.data[i] + "'"; } var str = "[" + res.data.join(",") + "]";</code>