![]() ![]() ![]() As a developer using JavaScript, you will certainly need to turn strings into an array. (Might be more appropriate to have an if (arr.length=0) to take some other action rather than running the SELECT statement. Convert a JavaScript Array into String with or without commas. If you intend to use the elements of your array as integers and not as strings after splitting the string, consider converting them into such. (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("','") + "'" 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). 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. The join () method will join all the elements of an array into a string by separating them with a specified character. quotedAndCommaSeparated = "'item1','item2','item3','item4'" For your quick answer, convert the Javascript array to string using join () method. Var quotedAndCommaSeparated = "'" + arr.join("','") + "'" Instead, one way to do it is as follows: var arr = This article will help you learn the easy ways you can convert an array to string in JavaScript: 1. ![]() 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. One option would be to pop the last item, then join all the rest by commas, and concatenate with and plus the last item: const input one, two, three, four const last input.pop () const result input. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |