![]() (Might be more appropriate to have an if (arr.length=0) to take some other action rather than running the SELECT statement. The toString method of arrays calls join() internally, which joins the array and returns one string containing each array element separated by commas. ![]() (And please tell me you're not using client-side JavaScript to form your SQL.)ĮDIT: to allow for an empty array, include a default value for the resulting string, otherwise (as pointed out by missingno) the string would be "''": var newString = arr.length = 0 ? "" : "'" + arr.join("','") + "'" Here are some of the most common methods: Using the join() method: This method joins all elements of an array into a string, separated by a specified separator. So if you specify a separator that includes the quotation marks and commas you just have to manually append a starting and ending quote for the first and last item (respectively). There are several ways to convert an array to a string with comma separation in Javascript. The split() method splits a string using a. javascript - How can I convert a comma-separated string to an array - Stack Overflow How can I convert a comma-separated string to an array Ask Question Asked 13 years, 2 months ago Modified 1 month ago Viewed 1.4m times 877 I have a comma-separated string that I want to convert into an array, so I can loop through it. The Array.join() method returns a string that is all of the array elements concatenated into a single string with an (optional) separator between each item. There is a built-in JavaScript method which you can use and convert a comma-separated string into an array. When you need to join together every item in a JavaScript array to form a comma-separated list of values, use. ![]() ![]() Var quotedAndCommaSeparated = "'" + arr.join("','") + "'" Instead, one way to do it is as follows: var arr = Use the join () method or toString () method to convert an array to a string with commas in JavaScript. The quotes aren't preserved because they're not actually part of the string value, they're just necessary to indicate string literals in your code. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |