简体   繁体   English

Javascript 2-d关联数组

[英]Javascript 2-d associative arrays

I have a Javascript 2-D array defined as follows: 我有一个Javascript 2-D数组,定义如下:

proData[prov_999998] = new Array();

proData[prov_999998]["address"] = "29 South St. South #202  Uxbridge   ON  L9P 1V9";

proData[prov_999998]["phone"] = "555-555-5555";

proData[prov_999998]["fax"] = "444-444-4444";

I'm trying to access these array elements in this function: 我试图在此函数中访问这些数组元素:

function switchPro(value) {
document.getElementById("letterheadName").value = value;
document.getElementById("letterheadAddress").value = providerData[prov_999998]["address"];
document.getElementById("letterheadAddressSpan").innerHTML = providerData[prov_999998]["address"] ;  
document.getElementById("letterheadPhone").value = providerData[prov_999998]["phone"];
document.getElementById("letterheadPhoneSpan").innerHTML = providerData[prov_999998]["phone"]; 
document.getElementById("letterheadFax").value = providerData[prov_999998]["fax"]; 
document.getElementById("letterheadFaxSpan").innerHTML = providerData[prov_999998]["fax"]; 
}

But I'm getting an error saying: "prov_999998 is undefined in "providerData[prov_999998]["address"];". Can someone please help me with this? I can't have numbers as array indexes because javascript does not accept array index starting with '0' such as [0999]. These array indexes are Ids entered by the user and I can't put a restriction on the Id format. I am new to javascript so I'd appreciate any help. 但我收到一条错误消息:“ providerData [prov_999998] [“ address”];中未定义prov_999998。”有人可以帮助我吗?我无法将数字作为数组索引,因为javascript不接受数组索引以'0'开头,例如[0999]。这些数组索引是用户输入的ID,因此我无法对ID格式进行限制。我是javascript的新手,所以我非常感谢您的帮助。

Actually, I want the 1st index to be a variable. 实际上,我希望第一个索引为变量。 This is what I was doing: 这是我在做什么:

function switchProvider(value) {
    var val =  "prov_"+value; 
    document.getElementById("letterheadName").value = value;
    document.getElementById("letterheadAddress").value = providerData[val]["address"];
    document.getElementById("letterheadAddressSpan").innerHTML = providerData[val]["address"] ; 
    document.getElementById("letterheadPhone").value = providerData[val]["phone"];
    document.getElementById("letterheadPhoneSpan").innerHTML = providerData[val]["phone"]; 
    document.getElementById("letterheadFax").value = providerData[val]["fax"]; 
    document.getElementById("letterheadFaxSpan").innerHTML = providerData[val]["fax"]; 

    } 

But this is not working, the values on the webpage are being rendered as 'undefined'. 但这不起作用,网页上的值被呈现为“未定义”。 What am I doing wrong? 我究竟做错了什么?

Here's more code, it's JSP and Javascript together. 这里是更多代码,它们是JSP和Javascript一起。

var providerData = new Array();
<% 
for (Provider p : prList) {
if (!p.getProviderNo().equalsIgnoreCase("-1")) {
        String prov_no = "prov_"+p.getProviderNo();
        %>
    providerData[<%=prov_no%>]["address"] = "<%=(p.getClinicAddress() + "  " + p.getClinicCity() + "   " + p.getClinicProvince() + "  " + p.getClinicPostal()).trim() %>";
    providerData[<%=prov_no%>]["phone"] = "<%=p.getClinicPhone().trim() %>";
    providerData[<%=prov_no%>]["fax"] = "<%=p.getClinicFax().trim() %>";

<%  } 
} %>

The syntax you use is for object key/value pairs, and not for arrays. 您使用的语法适用于对象键/值对,而不适用于数组。

You should most likely use 您最有可能使用

var proData = {};

proData['prov_999998'] = {};
proData['prov_999998']["address"] = "29 South St. South #202  Uxbridge   ON  L9P 1V9";
proData['prov_999998']["phone"] = "555-555-5555";
proData['prov_999998']["fax"] = "444-444-4444";

and access it with ( ie ) 与访问它(

providerData['prov_999998']["address"];

Now if prov_999998 is a variable then you should indeed access it without the quotes i added, but make sure that it has a value assigned, and that this value still persists when you try to use it in your function .. 现在,如果prov_999998是变量,那么您确实应该在不添加引号的情况下访问它,但是请确保已为其分配了一个值,并且当您尝试在函数中使用它时,该值仍然存在。


original answer 原始答案

You should wrap the prov_999998 in quotes 'prov_999998' to make it a literal, otherwise it is treated as a variable. 您应将prov_999998括在引号 'prov_999998'以使其成为文字,否则将其视为变量。

 
 
 
  
  proData['prov_999998'] = new Array(); proData['prov_999998']["address"] = "29 South St. South #202 Uxbridge ON L9P 1V9"; proData['prov_999998']["phone"] = "555-555-5555"; proData['prov_999998']["fax"] = "444-444-4444";
 
  

and

 
 
 
  
  function switchPro(value) { document.getElementById("letterheadName").value = value; document.getElementById("letterheadAddress").value = providerData['prov_999998']["address"]; document.getElementById("letterheadAddressSpan").innerHTML = providerData['prov_999998']["address"] ; document.getElementById("letterheadPhone").value = providerData['prov_999998']["phone"]; document.getElementById("letterheadPhoneSpan").innerHTML = providerData['prov_999998']["phone"]; document.getElementById("letterheadFax").value = providerData['prov_999998']["fax"]; document.getElementById("letterheadFaxSpan").innerHTML = providerData['prov_999998']["fax"]; }
 
  

The fact there are enough indexes to get to 999998 in a Javascript memory structure is worrisome. 在Javascript内存结构中有足够多的索引可以达到999998的事实令人担忧。 You're definitely going to knock a browser over if much of the preceding entries are loaded alongside it. 如果前面的很多条目都与浏览器一起加载,那么您肯定会让浏览器崩溃。

Is there anything wrong with calling the index 999 instead of 0999? 调用索引999而不是0999有什么问题吗?

It looks like you're trying to use strings to index the secondary items in the array, rather than indexes - like 'address' and 'phone'. 似乎您正在尝试使用字符串来索引数组中的次要项目,而不是索引-例如“地址”和“电话”。

So, your structure should be hashmaps of hashmaps: 因此,您的结构应该是哈希图的哈希图:

var providerData = {
    '0999': {
        address: '321 Sesame St',
        zip: '12345'
    },
    '1000': {// more here
    }
};

alert(providerData['0999']['address']); // alerts "321 Sesame St"

But you should consider the amount of data loaded into it. 但是您应该考虑加载到其中的数据量。

Guess your associative array is wrong (or your code lacks info). 猜测您的关联数组是错误的(或您的代码缺少信息)。

proData[prov_999998] = new Array();

should be 应该

proData['prov_999998'] = new Array();

and so on. 等等。 Anyhow, you could use javascript objects to do that in a neat way. 无论如何,您可以使用javascript对象以一种简洁的方式来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM