简体   繁体   中英

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']

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})`); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM