Javascript array to comma separated string with quotes

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.

So, don't use toString(). Instead, one way to do it is as follows:

var arr = ['item1','item2','item3','item4'];

var quotedAndCommaSeparated = "'" + arr.join("','") + "'";

// quotedAndCommaSeparated === "'item1','item2','item3','item4'"

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. 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).

(And please tell me you're not using client-side JavaScript to form your SQL.)

EDIT: 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("','") + "'";
// default for empty array here ---^^

(Might be more appropriate to have an if (arr.length===0) to take some other action rather than running the SELECT statement.)

Posted on Jan 14, 2021

Let's learn how to convert JavaScript array to string with or without commas. Example code included.

To convert a JavaScript array into a string, you can use the built-in Array method called toString.

The method will return a string that represents the elements stored in your array:

let numbers = [0, 1, 2, 3];
let numbersToString = numbers.toString();

console.log(numbersToString);
// output is "0,1,2,3"

Keep in mind that the toString method can’t be used on an array of objects because it will return [object Object] instead of the actual values.

To convert an array of objects into a string, you need to use the JSON.stringify method:

let users = [
  { name: "John" },
  { name: "Lisa" },
  { name: "Jack" },
  { name: "Mary" },
];
let usersToString = JSON.stringify(users);

console.log(usersToString);
// [{"name":"John"},{"name":"Lisa"},{"name":"Jack"},{"name":"Mary"}]"

It’s not really a representation of the array values because the return value will still have the square and curly brackets, but it’s the best you can do with an array of objects.

JavaScript array to string without commas

Sometimes you may need to convert your array into a string without the commas. The toString method has no parameter, so you need to use join method instead.

join accepts one string parameter which will serve as the separator for your string:

[1, 2, 3].join(); // "1,2,3"
[1, 2, 3].join("+"); // "1+2+3"
[1, 2, 3].join(" "); // "1 2 3"

You can even pass an empty string to the method:

[1, 2, 3].join(""); // 123

And that’s how you turn a JavaScript array into a string.

How to split an array into single quotes and comma separated list in PHP, When we are working dynamic SQL queries we have to build them using the post array, after splitting the array field key and values into separate array’s. we need to have single quote mark arround the values and it is separated with comma in sql INSERT statement like this.
INSERT INTO table_name (column1, column2, column3) VALUES (‘value1’, ‘value2’, ‘value3’).

Our goal is to achieve the highlighted text from the array, for this we are using implode function, appending single quote mark and comma followed by a space and othe single quote mark.( ‘, ‘ ). If we only use implode function it will insert the between the values, first quote mark and the last quote mark will be missed, for that we are appeding single quote mark at the string starting and the ending. Then we will ge the output what we wanted.

[php] $array = array(‘hi’, ‘this’, ‘is’, ‘an’, ‘array’);
$comma_list = "’" .implode("’, ‘", $array) . "’";
echo $comma_list;
?>
[/php]

How do you convert a string separated by a comma to an array?

Use the String. split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.

How to convert an array of strings to a string in JavaScript?

Convert Array to String in JavaScript.
Use the toString() Method to Convert Array to String in JavaScript..
Join the Elements of the Array Using .join() Method in JavaScript..
Use JSON.stringify() to Convert Array to String in JavaScript..
Use Type Coercing to Convert Array to String in JavaScript..

How to create string from array in JavaScript?

Array.prototype.join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

How to convert array to string without commas?

To convert an array to a string without commas, call the join() method on the array, passing it an empty string as a parameter - arr. join('') . The join method returns a string containing all array elements joined by the provided separator.