使用双引号加入JavaScript数组

时间:2013-07-08 16:01:27

标签: javascript google-maps

我正在尝试加入latLng列表,这些值存储在这样的数组中:

"48.2025,16.3687","48.2437,16.3797"

相反,我不断得到这样的输出:

"48.2025,16.3687,48.2437,16.3797"

这是我正在使用的代码:

$(document).ready(function()
    {

    $('#printMapLatLng').click(function()

        {
            var printPoints = [];
            for (var i in points) {
                var input = points[i].LatLng.toString();
                var output = ''+input.replace( /\s/g, "").slice(1,-1)+'';
                printPoints.push(output);
            }         
                var fullArray = printPoints.join();
                $(this).attr('href',PowerHour.getPrintUrl([fullArray]));
        });
});

3 个答案:

答案 0 :(得分:2)

这应该有帮助:)

var points = ["48.2025,16.3687","48.2437,16.3797"];
points.map(function(p){ return '"' + p + '"'; }).join(',');
// => '"48.2025,16.3687","48.2437,16.3797"'

答案 1 :(得分:1)

您可以这样做以获得输出

var fullArray = "\"" + printPoints.join("\",\"") + "\"";

答案 2 :(得分:0)

如果您要单行,请使用此:

const values = ["48.2025,16.3687", "48.2437,16.3797"];

console.log(values.map(value => `"${value}"`).join());