views:

26

answers:

1

Hello All,

I have a long string arrays, which looks like that

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

As you see, it contact Phone number and open time. I can use split to separet them into info=callinfo_data[n].split("#"); two sections, And then i can represent them in HTML like

"<div id ='phoneNumber'>"+info[0]+"</div><div id='openTime'>"+info[1]+"</div>"

But my display phone number function will read the cookie variables, and then select the right contact info to display.

Like,

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

My questions are how I can build a function to load phone number and time based on my cookie variables, UK in a smart way.

I can hard code everything, but i think it is too silly.

I have to write a long code like:

phone1= allinfo_data[0].split("#");

phone1= allinfo_data[1].split("#");
...

etc

Second questions, how can I load this long arrays into easy access multi arrays?

Thank you

Regards, Qing

A: 

Instead of using strings and splitting them why don't you use object literals to store your data?

Like this...

{
    "phone" : "111-111-1111",
    "hours" : "Monday - Friday: 9:00 a.m. to 7:30 p.m. GMT+1"
}

You can put these objects inside of an array:

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

You can then access them like this:

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);
};
Ronald