检查 XML HTTP 请求中的重复值

Checking for duplicate values in XML HTTP Request

我的任务是向现有应用程序添加功能。

目前它显示一个 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 returns stationareas.asp 中的 SQL 查询中列出的项目列表。

是否有可能在 areaStatus() 函数中以某种方式检查响应是否已经存在,如果存在,我们可以计算一下是否有 3 个 "Apple" 值,然后将该部分着色为绿色。但如果该部分有 >5 种颜色 "Purple"。

希望这是有道理的。任何帮助都会非常有帮助!

响应结构:

["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 中有许多关于 de-duplicating/counting 独特事件主题的 Stack Overflow 搜索结果。这是使用 O(n*n) 完成此操作的粗略方法,它也将构建您的 return 对象。

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]'

的示例输出

count: 5
fillColor: "green"
fillOpacity: 0.25
name: "C21"

你可以看到它在这个 JS 中工作 Fiddle: https://jsfiddle.net/igor_9000/8tbmw2fg/

希望对您有所帮助!