chozhan
chozhan

Reputation: 181

How to replace single quotes instead of double quotes in array of Javascript?

I have an array of string values with double quotes all i want is convert the array string value with single quotes.

Var arr=["abc123","cde345","ijk789"];

var test=[]
for(var i=0;i<arr.length;i++){
 ans=arr[i].replace(/"/g,"'")
  test.push(ans)
}

The test result supposed to be ['abc123','cde345','ijk789']

Upvotes: 1

Views: 4506

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386654

You could map the items with single quotes for a list and join it with comma.

var array = ["abc123", "cde345", "ijk789"],
    list = array.map(s => `'${s}'`).join(', ');

console.log(`select * from tableName where id in (${list})`);

Upvotes: 3

Related Questions