简体   繁体   English

如何在Javascript数组中替换单引号而不是双引号?

[英]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'] 测试结果应该是['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})`); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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