我有一个带有switch语句的函数,该函数查找单个情况,如果找不到,则运行default,这是另一个switch语句。它基本上是在整个数组中进行查找。我确定有更好的方法可以做到这一点,但是我对javascript还是陌生的。这是一个代码段:
for(var i=0; i < parks.length; i++) {
switch (parks[i][6]) {
case 'wc' :
wc.push(L.marker([parks[i][1], parks[i][2]], {icon: parks[i][4]}).bindpopup("<a href='" + parks[i][3] + "'>" + parks[i][0] + "</a>"));
break;
default :
switch (parks[i][7]) {
case 'grill' :
grill.push(L.marker([parks[i][1], parks[i][2]], {icon: parks[i][4]}).bindpopup("<a href='" + parks[i][3] + "'>" + parks[i][0] + "</a>"));
break;
default :
break;
break;
}
}
我的公园阵列示例,因为每个人都在问:
var parks = [
['Spårvagnsparken',59.3298868,18.0031605,'http://leksplay.com/sparvagnsparken', greenIcon, wc, grill, null, null, fence, null, null, null, null],
['Fredhällsparkens plaskdamm', 59.3320485,18.0029481, 'http://leksplay.com/fredhallsparkensplaskdamm', greenIcon, wc, null, null, null, null, null, null, water, null],
['Uggleparken', 59.3343715,18.0040208,'http://leksply.com/uggleparken', greenIcon, wc, null, null, null, null, pfence, null, null, null],
['Observatorielundens Parklek', 59.3413877,18.056007, 'http://leksplay.com/observatorielundensparklek', greenIcon, wc, null, null, null, null, pfence, null, null, toddler],
鉴于您提供的数组,我会
bindPopup
,不是bindPopup
bindpopup
构造为对象markers
{}
,"toddler"
等)"wc"
推入newMarker()
对象。markers
示例(仅用于对象,用于演示):
const markers = {};
const newMarker = poi => L.marker([poi.lat, poi.lng], {icon: poi.icon})
.bindPopup(`<a href="${poi.url}">${poi.name}</a>`);
const parks = [
['Spårvagnsparken', 59.3298868, 18.0031605, 'http://leksplay.com/sparvagnsparken', "greenIcon", "wc", "grill", null, null, "pfence", null, null, null, null],
['Fredhällsparkens plaskdamm', 59.3320485, 18.0029481, 'http://leksplay.com/fredhallsparkensplaskdamm', "greenIcon", "wc", null, null, null, null, null, null, "water", null],
['Uggleparken', 59.3343715, 18.0040208, 'http://leksply.com/uggleparken', "greenIcon", "wc", null, null, null, null, "pfence", null, null, null],
['Observatorielundens Parklek', 59.3413877, 18.056007, 'http://leksplay.com/observatorielundensparklek', "greenIcon", "wc", null, null, null, null, "pfence", null, null, "toddler"],
];
parks.forEach(([name, lat, lng, url, icon, ...props]) => {
props.filter(Boolean).forEach(prop => {
if (!(prop in markers)) markers[prop] = []; // Prepare if not exists
markers[prop].push(newMarker({name, lat, lng, url, icon}));
});
})
console.log(markers);
使函数调用并返回const markers = {};
const newMarker = poi => poi;
const parks = [
['Spårvagnsparken', 59.3298868, 18.0031605, 'http://leksplay.com/sparvagnsparken', "greenIcon", "wc", "grill", null, null, "pfence", null, null, null, null],
['Fredhällsparkens plaskdamm', 59.3320485, 18.0029481, 'http://leksplay.com/fredhallsparkensplaskdamm', "greenIcon", "wc", null, null, null, null, null, null, "water", null],
['Uggleparken', 59.3343715, 18.0040208, 'http://leksply.com/uggleparken', "greenIcon", "wc", null, null, null, null, "pfence", null, null, null],
['Observatorielundens Parklek', 59.3413877, 18.056007, 'http://leksplay.com/observatorielundensparklek', "greenIcon", "wc", null, null, null, null, "pfence", null, null, "toddler"],
];
parks.forEach(([name, lat, lng, url, icon, ...props]) => {
props.filter(Boolean).forEach(prop => {
if (!(prop in markers)) markers[prop] = []; // Prepare if not exists
markers[prop].push(newMarker({name, lat, lng, url, icon}));
});
})
console.log(markers);
,并在条件通过时在每次迭代时将其传递给数组。只需使用L.marker
检查索引处的项目是===
还是wc
:
grill
理想情况下,请修正输入结构,以便在位置const makeMarker = park => L.marker([park[1], park[2]], { icon: park[4] })
.bindpopup("<a href='" + park[3] + "'>" + park[0] + "</a>");
for (const park of parks) {
if (park[6] === 'wc') {
wc.push(makeMarker(park));
} else if (park[7] === 'grill') {
grill.push(makeMarker(park));
}
}
,[6]
等处,而不是在位置上使用不同的值,而在此处使用单个值数组,然后检查该数组是否包含不同的值,并将标记添加到对象数组:
[7]