我有一个名为value一个数组,当我console.log(value)我得到约30行代码有以下[6.443663, 3.419248]如何删除Javascript数组中的方括号?
的数字变化,因为它们是经纬度。但我需要以某种方式摆脱每个人的方括号。
我试过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])];
});
阵列尝试使用下面的代码
$.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);
}
});
});
+0
what * exactly *是'value'?一个数组,一个字符串? 'console.log(typeof value)'请。 –
+0
你的意思是说纬度/经度值是作为一个字符串存储在数组中,比如array [0] =“[23.021321,18.213123]”? –
+1
如果'value'是数组,则'value.toString()'与'value.join(',')'相同。 –