You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaScript中将XML解析的name与contextId以HashMap形式存储?

Absolutely, this is totally achievable in JavaScript! Instead of just concatenating values into text strings, we can create an object (which acts like a HashMap) where each name value is the key and its corresponding contextId is the value.

Key Fixes & Improvements to Your Code

First, let's address a small bug in your original code: in the second loop for name, you used i < y.length instead of j < y.length—that would have caused an infinite loop if x.length was larger than y.length.

More importantly, to correctly pair each name with its matching contextId, it's better to iterate over their parent projectArea elements (since your API endpoint targets workitem/projectArea/*). This ensures you don't accidentally mismatched values if the order of standalone contextId and name elements ever changes.

Here's the revised code:

function loadXMLDoc() { 
  var xmlhttp = new XMLHttpRequest(); 
  xmlhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
      myFunction(this); 
    } 
  }; 
  xmlhttp.open("GET", "/ccm/rpt/repository/workitem?fields=workitem/projectArea/*", true); 
  xmlhttp.send(); 
} 

function myFunction(xml) { 
  var xmlDoc = xml.responseXML; 
  // Create our "HashMap" (JavaScript object) to store name: contextId pairs
  const nameToContextId = {};

  // Get all projectArea elements (the parent of contextId and name)
  const projectAreas = xmlDoc.getElementsByTagName("projectArea");

  for (let i = 0; i < projectAreas.length; i++) {
    const area = projectAreas[i];
    
    // Extract contextId value (with fallback for missing nodes)
    const contextIdNode = area.getElementsByTagName("contextId")[0];
    const contextId = contextIdNode ? contextIdNode.textContent.trim() : null;
    
    // Extract name value (with fallback for missing nodes)
    const nameNode = area.getElementsByTagName("name")[0];
    const name = nameNode ? nameNode.textContent.trim() : null;
    
    // Only add to the map if both values exist
    if (name && contextId) {
      nameToContextId[name] = contextId;
    }
  }

  // Example usage: log the map or access specific values
  console.log("Name to Context ID Map:", nameToContextId);
  // Get contextId for a specific name:
  // console.log(nameToContextId["Your Project Name"]);
}

How It Works

  • We use a plain JavaScript object (nameToContextId) as our HashMap—this is the standard way to handle string-keyed key-value pairs in JS.
  • By iterating over each projectArea, we ensure we pair the correct contextId with its associated name (instead of grabbing all contextIds and names separately, which could lead to mismatches).
  • textContent.trim() is more reliable than childNodes[0].nodeValue because it automatically handles whitespace and nested text nodes.

If you need advanced HashMap features (like preserving insertion order, using non-string keys, or built-in iteration methods), you could also use the native Map object instead:

const nameToContextId = new Map();
// Then add pairs with:
nameToContextId.set(name, contextId);
// Access with:
nameToContextId.get(name);

内容的提问来源于stack exchange,提问作者Rajat Krishnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:08:15