问题->我的页面似乎处于无限循环或其他状态。它一直在加载,但我没有收到错误
目标->根据JSON字段中的ID,创建一个类型数组。我还创建了2个单独的数组(1个用于图层,1个用于颜色)。
我想为每种类型创建1层,并为其标记赋予唯一的颜色。
过程:我用以下方法声明我的3个数组:
var typeArray = [];
var colorArray = [];
var layerArray = [];'
之后,我浏览JSON文件在classificationid上的输出,并填充正常工作的类型数组。我记录了要验证的值
for( var i=0; i < jData.length; i++){
if(typeArray.includes(jData[i].classificationid)){
console.log(jData[i].classificationid + " is in array");
}
else{
typeArray.push(jData[i].classificationid);
}
}
之后,我遍历类型创建一个颜色和图层数组。颜色阵列和功能正常工作。我已经确认了。
for(var i = 0; i<typeArray.length; i++){
colorArray[i] = getRandomRgb();
layerArray[i] = L.layerGroup();
}
因此,基于此,我假设如果我有2种分类ID,我将拥有一个带有2个图层组的layerArray。 (一组对象)
在下面的代码中,我遍历整个JSON,其中嵌套了一个遍历typeArray的迭代。如果类型数组与分类匹配,则将div图标添加到相应的图层。 console.log(jData [i] .classificationid +“匹配” + typeArray [j]);向控制台提供正确的输入,之后我在控制台中看不到其他任何内容
for(var i=0; i < jData.length; i++){
console.log("I = " + i);
for(var j=0; i<typeArray.length; j++){
if(jData[i].classificationid == typeArray[j]){
console.log(jData[i].classificationid + " matches " + typeArray[j]);
L.marker([jData[i].latitudey, jData[i].longitudex],{
icon: L.divIcon({
html: '<i id ="icon1" class="fas fa-map-marker" style="font-size:48px;color:rgb(225, 80, 71)"></i>',
iconSize: [20, 20],
className: 'myDivIcon'
})
}).bindPopup(jData[i].addresscode).addTo(layerArray[j]);
}
}
我认为我的问题出在代码的某些地方。
问题1-标记未添加到layerArray 问题2-我不知道如何基于colorArray更改HTML rgb样式。
请帮助:(
答案 0 :(得分:2)
我认为您的循环卡在了这里
for(var j=0; i<typeArray.length; j++){
应该是
for(var j=0; j<typeArray.length; j++){