Javascript 二维关联数组
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的语法适用于对象键/值对,而不适用于数组。
您最有可能使用
(ie) 来使用和访问它。
现在,如果 prov_999998 是一个变量,那么您确实应该在不使用我添加的引号的情况下访问它,但请确保它已分配了值,并且这当您尝试在函数中使用它时,值仍然存在。
原始答案
您应该将 prov_999998 用引号括起来'prov_999998'
以使其成为文字,否则它将被视为变量。和
The syntax you use is for object key/value pairs, and not for arrays.
You should most likely use
and access it with (i.e.)
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.and
事实上,Javascript 内存结构中有足够的索引可以达到 999998,这一事实令人担忧。如果前面的大部分条目都与浏览器一起加载,那么您肯定会撞倒浏览器。
将索引称为 999 而不是 0999 有什么问题吗?
看起来您正在尝试使用字符串来索引数组中的次要项目,而不是索引 - 例如“地址”和“电话”。
因此,您的结构应该是哈希图的哈希图:
但您应该考虑加载到其中的数据量。
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:
But you should consider the amount of data loaded into it.
猜测您的关联数组是错误的(或者您的代码缺少信息)。
应该是
等等。无论如何,您可以使用 javascript 对象以一种简洁的方式来做到这一点。
Guess your associative array is wrong (or your code lacks info).
should be
and so on. Anyhow, you could use javascript objects to do that in a neat way.