我有一个很长的字符串数组,看起来像
var callinfo_data=new Array(
"1300 135 604#<b>Monday - Friday: 9:00 a.m. to 5:30 p.m. AEST</b>", //Australia
..
"0844000040#<b>lunedì-venerdì ore 10:00 - 17:00 CET</b>", //Switzerland (it)
"212 356 9707#<b>Hafta içi her gün: 10:00 - 18:00</b>", //Turkey
"08451610009#<b>Monday - Friday: 9:00 a.m. to 6:30 p.m. GMT</b>", //UK
"866 486 6866#<b>Monday - Friday: 7:00 a.m. to 11:00 p.m. EST</b><br />Saturday: 9:00 a.m. to 8:00 p.m. EST", //USA
"+31208501004#<b>Monday - Friday: 9:00 a.m. to 7:30 p.m. GMT+1</b>", //other countries
" # ");
如您所见,它会联系电话号码和开放时间。我可以使用拆分将它们分成几部分 信息= callinfo_data [n]的.split( “#”); 两节, 然后我可以用HTML代表它们
"<div id ='phoneNumber'>"+info[0]+"</div><div id='openTime'>"+info[1]+"</div>"
但我的显示电话号码功能会读取cookie变量,然后选择要显示的正确联系信息。
喜欢,
phone=callinfo_data[2].split("#");
if (locale == 'UK') details = phone[0]+ build_dropdown(locale);
else if (locale == 'fr') details = 'French Contact Details<br>'+build_dropdown(locale);
else if (locale == 'be') details = 'Belgian Contact Details<br>'+ build_dropdown(locale);
else details = 'Unknown Contact Detail';
writeContactInfo(details);
我的问题是如何基于我的cookie变量,英国以智能的方式构建一个功能来加载电话号码和时间。
我可以对所有内容进行硬编码,但我觉得它太傻了。
我必须编写一个长代码,如:
phone1= allinfo_data[0].split("#");
phone1= allinfo_data[1].split("#");
...
等
第二个问题,我如何将这个长数组加载到易于访问的多数组中?
谢谢
此致 清
答案 0 :(得分:0)
为什么不使用对象文字来存储数据,而不是使用字符串并拆分它们?
喜欢这个......
{
"phone" : "111-111-1111",
"hours" : "Monday - Friday: 9:00 a.m. to 7:30 p.m. GMT+1"
}
您可以将这些对象放在数组中:
var locales = {
"UK" : {
"details": "UK Contact Info",
"phone" : "111-111-1111",
"hours" : "Monday - Friday: 9:00 a.m. to 7:30 p.m. GMT+1"
},
"DE": {
"details": "DE Contact Info",
"phone" : "111-111-1111",
"hours" : "Monday - Friday: 9:00 a.m. to 7:30 p.m. GMT+1"
}};
然后您可以像这样访问它们:
locales["UK"]["phone"];
function buildTimeInfo(){
var newDiv = document.createElement("div");
var html = "<h2>" + locales[locationFromCookie]["details"] + "</h2>";
html += "<h3>" + locales[locationFromCookie]["hours"] + "</h3>";
html += "<p>" + locales[locationFromCookie]["phone"] + "</p>";
newDiv.innerHTML = html;
document.body.appendChild(newDiv);
};