Javascript 二维关联数组

发布于 2024-11-02 01:25:38 字数 2558 浏览 4 评论 0原文

我有一个 Javascript 二维数组,定义如下:

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";

我试图在此函数中访问这些数组元素:

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"]; 
}

但我收到一条错误消息:“prov_999998 在“providerData[prov_999998][“address”] 中未定义; ”。 有人可以帮我解决这个问题吗?我不能将数字作为数组索引,因为 javascript 不接受以“0”开头的数组索引,例如 [0999]。这些数组索引是用户输入的Id,我无法对Id格式进行限制。我是 javascript 新手,所以我会很感激任何帮助。

实际上,我希望第一个索引是一个变量。这就是我正在做的:

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"]; 

    } 

但这不起作用,网页上的值被呈现为“未定义”。我做错了什么?

这里有更多代码,它是 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() %>";

<%  } 
} %>

I have a Javascript 2-D array defined as follows:

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.

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.

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() %>";

<%  } 
} %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒与心事 2024-11-09 01:25:38

您使用的语法适用于对象键/值对,而不适用于数组。

您最有可能使用

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";

(ie) 来使用和访问它。

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

现在,如果 prov_999998 是一个变量,那么您确实应该在不使用我添加的引号的情况下访问它,但请确保它已分配了值,并且这当您尝试在函数中使用它时,值仍然存在。


原始答案

您应该将 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";

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 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 (i.e.)

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 ..


original answer

You should wrap the prov_999998 in quotes 'prov_999998' to make it a literal, otherwise it is treated as a variable.

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"]; 
}

一场春暖 2024-11-09 01:25:38

事实上,Javascript 内存结构中有足够的索引可以达到 999998,这一事实令人担忧。如果前面的大部分条目都与浏览器一起加载,那么您肯定会撞倒浏览器。

将索引称为 999 而不是 0999 有什么问题吗?

看起来您正在尝试使用字符串来索引数组中的次要项目,而不是索引 - 例如“地址”和“电话”。

因此,您的结构应该是哈希图的哈希图:

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

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

但您应该考虑加载到其中的数据量。

The fact there are enough indexes to get to 999998 in a Javascript memory structure is worrisome. 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?

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.

一抹苦笑 2024-11-09 01:25:38

猜测您的关联数组是错误的(或者您的代码缺少信息)。

proData[prov_999998] = new Array();

应该是

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

等等。无论如何,您可以使用 javascript 对象以一种简洁的方式来做到这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文