JavaScript中如何用撇号和逗号连接数组



我想不出解决问题的最佳方法。我希望将一组数字连接到一个数组中,并在其后面加上"。

例如:

' 1234 ', ' 5678 ', '…

我设法使它与逗号一起工作:

var radio_comma = document.getElementById("optionsRadios1").value;
if (document.getElementById("optionsRadios1").checked) {
        sku_output = txtArray.join(radio_comma);
} 

我不知道如何用'number'来修改它,

I did attempt this with

sku_output = radio_singlequote.concat(x, radio_singlequote, radio_comma);

但是它会输出'123,123,123,123,123'

sku_output = "'" + txtArray.join("','") + "'"应该给出您所搜索的输出。

你可以为它做一个Array扩展

Array.prototype.toSingleQuotedString = function () {
  return "'" + this.join("','") + "'";
}
//=> sku_output = txtArray.toSingleQuotedString();

另一个角度使用Array.map(见MDN)

sku_output = txtArray.map( function (a) { return "'"+a+"'"; } ).join(',');

最新更新