简体   繁体   English

用双引号连接 JavaScript 数组

[英]Join a JavaScript array with double quotes

I'm trying to join a list of latLng values which are stored in an array like this:我正在尝试加入一个latLng值列表,这些值存储在这样的数组中:

"48.2025,16.3687","48.2437,16.3797"

Instead, I keep getting output like this:相反,我不断得到这样的输出:

"48.2025,16.3687,48.2437,16.3797"

This is the code that I'm using:这是我正在使用的代码:

$(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]));
        });
});

This should help :)这应该有帮助:)

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

您可以这样做以获得输出

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

If you want single line, use this:如果你想要单行,使用这个:

 const values = ["48.2025,16.3687", "48.2437,16.3797"]; console.log(values.map(value => `"${value}"`).join());

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

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