我的任务是向现有应用程序添加功能。
目前,它显示了一个Google地图,其中如果XML HTTP响应中没有代码,则使用坐标的特定区域的叠加层和使用坐标的区域将以红色标记。然后,如果有一个值,则将其涂成绿色。
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
stationground = xmlhttp.responseText.split(",");
}
}
xmlhttp.open("GET","stationareas.asp",true);
xmlhttp.send();
console.log(stationground);
}
function areaStatus() {
loadXMLDoc();
map.data.setStyle( function(feature) {
var featurecountry = feature.getProperty('letter');
if (stationground.indexOf(featurecountry) != -1) {
return/** @type {google.maps.Data.StyleOptions} */ {
fillColor: 'red' , fillOpacity: 0.25 };
} else {
return/** @type {google.maps.Data.StyleOptions} */ {
fillColor: 'green' , fillOpacity: 0.25 };
}
console.log(featurecountry);
});
}
console.log返回stationareas.asp的SQL查询中列出的项目列表。
是否可以在
areaStatus()
函数中以某种方式检查响应是否已经存在,如果可以,我们可以算出这是否为“ Apple”的3个值,然后将该部分涂为绿色。但是如果颜色> 5,则该部分为“紫色”。希望这是有道理的。任何帮助将非常有帮助!
回应结构:
[“ C04”,“ C04”,“ C09”,“ C21”,“ C24”,“ C26”,“ C43”,“ C46”,“ C46”,“ C66”,“ C68”,“ C21”,“ C09”,“ C21”,“ C21”,“ C21”,“ E10”,“ E11”,“ E13”,“ E14”,“ E20”,“ E20”,“ E22”,“ E26”,“ G10” ,“ G10”,“ G10”,“ G10”,“ G10”,“ G10”,“ G10”,“ G10”,“ G23”,“ G38”,“ G38”,“ G60”,“ G60”,“ G60”,“ G10”,“ G10”,“ G10”,“ G60”,“ L15”,“ L30”,“ L30”,“ L30”,“ L31”,“ L32”,“ L32”,“ L35” ,“ L55”,“ L55”,“ L72”,“ L95”,“ L30”,“ L30”,“ L55”,“ L30”,“”]
最佳答案
您需要对响应数组进行重复数据删除,并计算每个结果的出现次数。
关于JavaScript中唯一重复项的重复数据删除/计数,有很多Stack Overflow搜索结果。这是使用O(n * n)进行操作的一种粗略方法,该方法也会构建返回对象。
的JavaScript
// update the colors
function updateColors(obj) {
if(obj.count == 1) {
obj.fillColor = "red";
} else if(obj.count < 3) {
obj.fillColor = "yellow";
} else if(obj.count < 10) {
obj.fillColor = "green";
} else {
obj.fillColor = "blue";
}
}
// your test data
var responses = ["C04", "C04", "C09", "C21", "C24", "C26", "C43", "C46", "C46", "C66", "C68", "C21", "C09", "C21", "C21", "C21", "E10", "E11", "E13", "E14", "E20", "E20", "E22", "E26", "G10", "G10", "G10", "G10", "G10", "G10", "G10", "G10", "G23", "G38", "G38", "G60", "G60", "G60", "G10", "G10", "G10", "G60", "L15", "L30", "L30", "L30", "L31", "L32", "L32", "L35", "L55", "L55", "L72", "L95", "L30", "L30", "L55", "L30", ""];
// a new array to track the objects
var results = [];
// for every element in your response
for (var i = 0; i < responses.length; i++) {
// see if there is an existing match
var found = false;
// loop over the existing results
for( var j = 0; j < results.length; j++ ) {
// if the current response matches an existing result, update the count
if(results[j].name == responses[i]) {
results[j].count++;
updateColors(results[j]); //update the colors
found = true; //set the flag to true, so we dont add this as a new result
j = results.length; //exit the loop
}
}
// if the response element wasnt matched, add it as a new result
if(!found) {
results.push({name: responses[i], count: 1, fillColor: 'red', fillOpacity: 0.25})
}
}
//print everything
console.log(results)
“ results [2]”的示例输出
数:5
fillColor:“绿色”
fillOpacity:0.25
名称:“ C21”
您可以在此JS小提琴中看到它的工作原理:https://jsfiddle.net/igor_9000/8tbmw2fg/
希望有帮助!