Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Multi-Dimensional Array with Custom Index?

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
Is it possible to create a multidimensional array that uses a string as the index instead of 0,1,2 (a number)?

I'm looking to do something like this:
Code:
var myArray = new Array(
    new Array("index1"=>"value1","index2"=>"value2"),
    new Array("index1"=>"value1","index2"=>"value2")
}

the main array can use integers, but I would like to reference the values by a string for the 2nd level arrays.

Thanks.
 
Sure:

Code:
var arr = {
   abc: 1,
   def: {
      xyz: 8,
      rst: 'wibble'
   },
   ghi: 'hello world'
}

alert(arr['abc']); // should show '1'
alert(arr['def']['xyz']); // should show '8'
alert(arr['def']['rst']); // should show 'wibble'

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Oops - I should have also mentioned that to loop around these you can use a 'for ... in' construct:

Code:
for (items in arr) {
   alert('arr[\'' + items + '\'] = ' + arr[items]);
}

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok, 2 more questions:

1. When I try this, I get: "invalid property id"
ex:
Code:
var custom_array = {
    {   fieldName: "field1",
        otherVar: "stuff",
        lastVar: "finished" },
    {   fieldName: "field2",
        otherVar: "stuff and junk",
        lastVar: "finished again" }
};

2. If I wanted to store a Regular Expression pattern in one of these (for example the lastVar spot), what format do it store it in?
 
Your inner "array" doesn't have a property name within the first. Follow the examples I gave.

To answer your question - however you want to. As a strinng, or as a regexp. Whichever suits.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
ok, on the "storing regular expressions" topic, before I was using (to validate US Phone numbers):
Code:
thisRegExp = /^(\(?[0-9]{3}[\)-\. ]?\ ?)?[0-9]{3}[-\. ]?[0-9]{4}$/;
if(thisRegExp.test(the_value)){
...
And that worked fine.

Now I'm storing the values like this:
Code:
usphone: {
    className: "usphone",
    defaultMessage: "This field must contain a valid US phone number.",
    regExp: "^(\(?[0-9]{3}[\)-\. ]?\ ?)?[0-9]{3}[-\. ]?[0-9]{4}$"
}
And when I run it like this:
Code:
thisRegExp = new RegExp(window['custom_variables'][valSelector]['regExp']);
if(thisRegExp.test(the_value)){
...
I start to have problems. This particular regular expression gives me the message: invalid quantifier

A different one for a credit card number continues to return false, but these all worked using the // method. So, I tried:
Code:
thisRegExp = "/" + window['custom_variables'][valSelector]['regExp'] + "/";
But then I get: thisRegExp.text is not a function.

What am I doing wrong?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top