Results 1 to 5 of 5

Thread: Quick Q: Grabbing Elements from Array

  1. #1
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quick Q: Grabbing Elements from Array

    Hi,

    I am having trouble with this:

    document.formname."Course_"+no.value;

    As you can see, the name of the form field I want to grab depends on a previous variable: "no"

    and therefore the name of the form element is:

    "Course_"+no

    How can I do this using JS?

    Thank you for your help!!

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JamesAU
    I am having trouble with this:

    document.formname."Course_"+no.value;
    One can construct property names at run-time using square bracket notation:

    Code:
    document.forms.formname.elements['Course_' + no.value]
    The expression between the brackets will be evaluated (a string concatenation in this case), then converted to a string. It is this string that's used as the property name.

    I'd like to point out that this has nothing to do with arrays. It's a general language feature and works with any object.

    Hope that helps,
    Mike

  3. #3
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Mike,

    That's a big help.

    Thank you very much.

    One more quick thing, if you have a sec.

    In PHP, I can define an array as follows: $array = ('value1'=>'1', 'value2'=>'2');

    This is a much easier way to specify custom indexes for my array.

    Is there a similar way to do this in JS?

    Thanks.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JamesAU
    In PHP, I can define an array as follows: $array = ('value1'=>'1', 'value2'=>'2');
    I though that might come up, which is why I made the note about not being array specific.

    First, you need to consider if this is really an array. In PHP, arrays are associative in that indices can be both numbers and strings, and the array functions apply equally either way. This is not true of Javascript (or more formally, ECMAScript) as there is no concept of associative arrays. This is a source of some confusion, particularly as some sites try to convince readers of the contrary.

    Arrays, and Array methods, work only with numeric indicies. The indices can be strings that represent numbers (like '1' and '54', but not '01' or '0x32'), but they are all still essentially numbers, nonetheless. For example,

    Code:
    var a = new Array();
    
    a[0] = 'some value';
    creates an array and assigns it one element. The length property of that array will be one (1), and all Array methods will operate based on that one element only.

    Code:
    a[2] = 'some other value';
    adds another element, but as arrays are sparse (you can skip elements), this will actually increase the length property to three, and Array methods will behave as if there were three elements. The value of a[1] will be the primitive type, undefined.

    Hopefully, so far so good. However,

    Code:
    a['key'] = 'yet another value';
    tends to throw a spanner in the works. Some people expect that this will be treated as another array element, but it won't. The length property will be unaltered, and none of the Array methods will consider this property when they are called.

    As I said in my previous post, this behaviour is a language feature and any object - Array, Boolean, Math, Number, Object, RegExp, String, and even host objects (like HTML elements) - can exhibit the effects.

    So, back to the question I asked above: do you actually want an array(-like) object, or just some container that you can shove values into and access them by name? To do the former properly, you'll need to produce a fair amount of code to result in a decent hashtable implementation (I have one, but I should probably revise it thoroughly and remove some of its deficiencies). If you just want a simple container, you can use an object literal:

    Code:
    /* The literal characters are braces. */
    var container = {key1 : 'value1',
                     key2 : 'value2'};
    If you want to use characters that can't appear in identifiers (like spaces, or property names begining with numbers), the surround the property name in quotes ('some key', for example).

    Hope that helps,
    Mike

  5. #5
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Mike,

    Thanks for that, makes perfect sense.

    - James

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •