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

javascript - Front-end assembly string problem ==

WBOY
Release: 2016-09-21 14:12:58
Original
877 people have browsed it

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>
Copy after login
Copy after login

['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>
Copy after login
Copy after login

['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>
Copy after login

<code>for(var i = 0; i < res.data.length; i++) {
  res.data[i] = "'" + res.data[i] + "'";
}
var str = "[" + res.data.join(",") + "]";</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template