我正在尝试编写一段创建数组的Javascript。 在数组内部,语法是严格的,因为它需要由MapViewer解析。 但是,我似乎无法找到为什么每次我将一个元素推入我的数组时,这个元素都有引号?
这是我的代码:
var i = 0;
var j = 0;
var tempMarkers = [];
while (clusters.length - i > 0) {
if (i % 3 == 0) {
for (j = 0; j < clusters[i].length; j++) {
tempMarkers.unshift("{latitude:" + tempLocationsArray[clusters[i][j]][1] + ",longitude:" +
tempLocationsArray[clusters[i][j]][0] + ",tintColor: MapView.PinColors.GREEN}");
}
} else if (i % 3 == 1) {
for (j = 0; j < clusters[i].length; j++) {
tempMarkers.unshift("{latitude:" + tempLocationsArray[clusters[i][j]][1] + ",longitude:" +
tempLocationsArray[clusters[i][j]][0] + ",tintColor: MapView.PinColors.RED}");
}
} else {
for (j = 0; j < clusters[i].length; j++) {
tempMarkers.unshift("{latitude:" + tempLocationsArray[clusters[i][j]][1] + ",longitude:" +
tempLocationsArray[clusters[i][j]][0] + ",tintColor: MapView.PinColors.PURPLE}");
}
}
i++;
}
console.log(tempMarkers);
&#13;
这就是我在控制台中看到的:
["{latitude:37.33182081,longitude:-122.03038642,tintColor: MapView.PinColors.RED}",
"{latitude:37.33178632,longitude:-122.0306262,tintColor: MapView.PinColors.GREEN}",
"{latitude:37.33162007,longitude:-122.03070577,tintColor: MapView.PinColors.PURPLE}",
"{latitude:37.33142585,longitude:-122.03072774,tintColor: MapView.PinColors.RED}",
"{latitude:37.33124551,longitude:-122.03073664,tintColor: MapView.PinColors.GREEN}",
"{latitude:37.33108059,longitude:-122.03068245,tintColor: MapView.PinColors.PURPLE}",
"{latitude:37.33091383,longitude:-122.03061321,tintColor: MapView.PinColors.RED}",
"{latitude:37.3307498,longitude:-122.03054302,tintColor: MapView.PinColors.GREEN}",
"{latitude:37.33069778,longitude:-122.03035543,tintColor: MapView.PinColors.PURPLE}",
"{latitude:37.33067203,longitude:-122.03018068,tintColor: MapView.PinColors.RED}",
"{latitude:37.33067784,longitude:-122.02998825,tintColor: MapView.PinColors.GREEN}",
"{latitude:37.33068906,longitude:-122.02976028,tintColor: MapView.PinColors.PURPLE}",
"{latitude:37.33070167,longitude:-122.02952527,tintColor: MapView.PinColors.RED}",
"{latitude:37.33045837,longitude:-122.02866686,tintColor: MapView.PinColors.GREEN}",
"{latitude:37.32795258,longitude:-122.01982651,tintColor: MapView.PinColors.PURPLE}",
"{latitude:37.32465631,longitude:-122.02308996,tintColor: MapView.PinColors.RED}",
"{latitude:37.32632152,longitude:-122.02614004,tintColor: MapView.PinColors.GREEN}",
"{latitude:37.33070542,longitude:-122.02915124,tintColor: MapView.PinColors.PURPLE}",
"{latitude:37.33019844,longitude:-122.02479669,tintColor: MapView.PinColors.RED}",
"{latitude:37.32463343,longitude:-122.0228739,tintColor: MapView.PinColors.GREEN}"
]
你看到我有这些报价的原因吗?
答案 0 :(得分:2)
你用双引号将它作为字符串传递,这就是为什么它在那里显示它。这不是因为不合时宜。
tempMarkers.unshift("{latitude:" + tempLocationsArray[clusters[i][j]][1] + ",longitude:" +
尝试以下创建哈希的语法以避免双引号。
tempMarkers.unshift({"latitude": tempLocationsArray[clusters[i][j]][1] , "longitude": tempLocationsArray[clusters[i][j]][0], "tintColor": MapView.PinColors.GREEN});