我的任务是向现有应用程序添加功能。
目前,它显示了一个谷歌地图,如果 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()
函数中以某种方式检查响应是否已经存在,如果存在,我们可以计算出是否有 3 个“Apple”值,然后将该部分着色为绿色。但如果有 >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)
“结果[2]”的输出示例
count: 5
fillColor: "green"
fillOpacity: 0.25
name: "C21"
你可以看到它在这个 JS Fiddle 中工作:https://jsfiddle.net/igor_9000/8tbmw2fg/
希望有帮助!
关于javascript - 检查 XML HTTP 请求中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37970291/