gpt4 book ai didi

javascript - 为什么Javascript unshift方法添加引号?

转载 作者:行者123 更新时间:2023-12-02 14:18:35 24 4
gpt4 key购买 nike

我正在尝试编写一段创建数组的 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);

这就是我在控制台中看到的内容:

["{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}"
]

你明白我引用这些引言的原因吗?

最佳答案

您将其作为带双引号的字符串传递,这就是它在那里显示的原因。这不是因为 unshift。

 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});

关于javascript - 为什么Javascript unshift方法添加引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38845377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com