简体   繁体   English

如何删除Javascript数组中的方括号?

[英]How to remove square brackets in a Javascript array?

I have an array called value and when I console.log(value) I get about 30 lines of code with the following [6.443663, 3.419248]我有一个名为value的数组,当我使用console.log(value)我得到大约 30 行代码,其中包含以下[6.443663, 3.419248]

The numbers change as they are Latitudes and Longitudes.数字随着纬度和经度的变化而变化。 But I need to somehow get rid of the square brackets around each one.但我需要以某种方式摆脱每个人周围的方括号。

I tried var replaced = value.toString().replace(/\\[.*\\]/g,'');我试过var replaced = value.toString().replace(/\\[.*\\]/g,''); but this changed the above example to 6.443407,3.419035000000008 and failed my code.但这将上面的示例更改为6.443407,3.419035000000008并且我的代码失败了。

In other words I need help getting rid of square brackets in an array in order to plot points on my map.换句话说,我需要帮助去掉数组中的方括号,以便在我的地图上绘制点。

My array is made up from the following code because each latitude and longitude value is held within onClick functions within a links.我的阵列被从下面的代码,因为每个纬度和经度值内保持由onClick内功能a链接。 So a member on here kindly supplied the below code to get my values into an array but now I'm struggling to get the values out without any brackets ruining it.所以这里的一个成员好心地提供了下面的代码来将我的值放入一个数组中,但现在我正在努力将值取出而没有任何括号破坏它。

var obj = {};
$('.choice-location-inner a').each(function(){
    var $this = $(this);
    var text = $this.attr('onClick').replace('findLocation(\'', '').replace('\');return false;', '');
    var array = text.split(',')
    obj[this.id]= [parseFloat(array[0]), parseFloat(array[1])];
});

The array is trying to plot markers on my Google Map using the following code该数组正在尝试使用以下代码在我的 Google 地图上绘制标记

$.each(obj , function(index, value){
    geocoder = new google.maps.Geocoder();

    geocoder.geocode( { 'address': value}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            marker = new google.maps.Marker({
                map: map,
                icon: image,
                position: results[0].geometry.location
            });
        } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            wait = true;
            setTimeout("wait = false", 1000);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
});

transforms the line to array to text.将行到数组转换为文本。

var value = [[1234213, 1234231], [1234213, 1234231]];
var dato = value[0];
console.log(dato.toString());

Unless I've misread and the item is an object rather than a string with brackets contained, the you could just access it by its sub array poition除非我误读了并且该项目是一个对象而不是包含括号的字符串,否则您可以通过其子数组 poition 访问它

lat = obj[this.id][0]
lng = obj[this.id][1]

perhaps?可能?

It looks like you want the comma separated string representation of the array.看起来您想要数组的逗号分隔字符串表示形式。 If that's the case, you can do this:如果是这种情况,您可以这样做:

var commasep = value.join(",");
`${arr.map(({ id }) => id)}`

也许这种方式比使用内置函数简单很多!

If you need a clean input you can write this.如果你需要一个干净的输入,你可以写这个。

var justTheDataWithoutSeparation = data.split('');

of course, if you want to separate with commas or something else, you need to add当然,如果你想用逗号或其他东西分隔,你需要添加

vat dataSeparatedWithSomething = data.split('-');

You need to be careful because doing this method can cause your data to set like string value, and may affect your output, you need to convert that before.您需要小心,因为执行此方法会导致您的数据设置为字符串值,并可能影响您的输出,您需要在此之前对其进行转换。

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

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