我有一个名为value
的数组,当我使用console.log(value)
时,会得到大约30行代码,其中包含以下内容:[6.443663, 3.419248]
这些数字会随着纬度和经度而改变。但是我需要以某种方式消除每个数字周围的方括号。
我尝试过var replaced = value.toString().replace(/\[.*\]/g,'');
但这将上面的示例更改为6.443407,3.419035000000008
并使我的代码失败。
换句话说,我需要帮助消除数组中的方括号,以便在地图上绘制点。
我的数组由以下代码组成,因为每个纬度和经度值都存储在a
链接中的onClick
函数中。因此,本站的一位成员友好地提供了下面的代码,以将我的值获取到数组中,但现在我无法将其中的值取出,因为括号破坏了它。
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])];
});
数组正试图使用以下代码在我的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);
}
});
});
console.log(typeof value)
。 - gen_Ericvalue
是数组,则value.toString()
等同于value.join(',')
。 - gen_Ericconsole.log(typeof value)
返回值为Object
。 - ngplayground