Home >Backend Development >PHP Tutorial >javascript - Front-end assembly string problem ==

javascript - Front-end assembly string problem ==

WBOY
WBOYOriginal
2016-09-21 14:12:58870browse

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

Reply content:

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 quotes
JSON.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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn