【问题标题】:Checking for duplicate values in XML HTTP Request检查 XML HTTP 请求中的重复值
【发布时间】:2016-10-24 12:41:32
【问题描述】:

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

目前,如果 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”、“”]

【问题讨论】:

  • 我没有尝试任何实质性的东西。起初我尝试使用 IndexOf() 但意识到这不是方法。我一直在研究并没有遇到任何有帮助的东西。我可以计算有多少个值,但我需要检查每个值是否有这么多单独的响应。如果这是有道理的。就像我上面的 Apple 示例一样。
  • 如果不知道您的 XML 响应的结构和值是什么,这个问题是无法真正回答的。如果它是一个数组,只需计算出现次数。这样的事情会让你开始:stackoverflow.com/questions/17313268/…
  • 谢谢。我会看看。我添加了回复的结构。

标签: javascript html xml google-maps asp-classic


【解决方案1】:

您需要对响应数组进行重复数据删除,并计算每个结果的出现次数。

有很多 Stack Overflow 搜索结果是关于在 JavaScript 中重复数据删除/计数唯一事件的主题。这是一种使用 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]”的示例输出

计数:5
填充颜​​色:“绿色”
填充不透明度:0.25
名称:“C21”

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

希望有帮助!

【讨论】:

  • 谢谢你。我将尝试实现这一点。一旦我得到一些工作,我会接受它作为答案!再次感谢。
猜你喜欢
  • 2019-04-26
  • 1970-01-01
  • 2018-07-31
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多