using for to read an array with string indices

Discussion in 'Javascript' started by Randell D., Nov 22, 2003.

  1. Randell D.

    Randell D. Guest

    Folks,

    I can program fairly comfortably in PHP and can, for the most part using
    these skills and others that I've picked up over the years manage to
    read/understand most code in Javascript... so I'm just asking for a few
    pointers (or the full solution if you have the time) for what I want to do.

    Basically, I want to write a javascript wherby I only need to pass it the
    names of form fields - then my javascript will check each form field has a
    value greater than zero in length - Any field value that fails this test is
    noted... when all fields have been tested, an alert box opens telling the
    user that they forgot to fill in 'x' number of fields and the field names
    are named as they would appear visably on the form. I can write most of the
    script but there is a method that I would like to do but don't know how to
    implement it in Javascript...

    A brief example of a form field would be the following HTML:

    Street Name: <input type=text size=30 name=streetName>


    My solution to alert the user in a user friendly manner would be to have an
    array with string indices. Thus the following piece of javascript:

    var requiredfields=new Array();
    requiredfields['streetName']="Street Name";


    The reason why I have it setup like this is I can use the index of an
    element to check a specific field in a form. If that field string length is
    zero, then the alert box will report "Street Name" and not "streetName"...
    My question is: How can I create a loop that will give me the element name?
    In PHP I could do something like :

    foreach($requiredFields as $inputTagName=>$realFieldName)
    { print("<br>Input tag $inputTagName is known by the user as
    $realFieldName"); }

    How can I achieve this in Javascript? How can I loop thru a string indexed
    array? It does not have to be a for loop... but I do want to have the real
    value of the array element, and the array element value...

    Can anybody help me out? I'm still playing with the rest of my scrpit - I'm
    confident that I can do the rest...

    Thanks in advance, replies please via the newsgroup... it might help someone
    else...
    randell d.
     
    Randell D., Nov 22, 2003
    #1
    1. Advertising

  2. Randell D.

    Eric Bohlman Guest

    "Randell D." <> wrote in
    news:ypQvb.462767$6C4.34453@pd7tw1no:

    > My solution to alert the user in a user friendly manner would be to
    > have an array with string indices. Thus the following piece of
    > javascript:
    >
    > var requiredfields=new Array();
    > requiredfields['streetName']="Street Name";


    In Javascript, "associative arrays" are implemented by Object rather than
    Array. So make that first line:

    var requiredfields=new Object();

    The second statement will work, but you're going to be adding several
    fields, so look up "object literals" in a good JS reference to learn a way
    to do it with much less typing, as in:

    var requiredfields = {
    streetname:"Street Name",
    city:"City",
    state:"State or Province"
    };

    > The reason why I have it setup like this is I can use the index of an
    > element to check a specific field in a form. If that field string
    > length is zero, then the alert box will report "Street Name" and not
    > "streetName"... My question is: How can I create a loop that will give
    > me the element name? In PHP I could do something like :
    >
    > foreach($requiredFields as $inputTagName=>$realFieldName)
    > { print("<br>Input tag $inputTagName is known by the user as
    > $realFieldName"); }
    >
    > How can I achieve this in Javascript? How can I loop thru a string
    > indexed array? It does not have to be a for loop... but I do want to
    > have the real value of the array element, and the array element
    > value...


    Use the "for (<variable> in <object>)" syntax:

    for (var inputTagName in requiredfields) {
    document.write("<br>Input tag "+inputTagName+
    " is known by the user as "+requiredfields[inputTagName]);
    }
     
    Eric Bohlman, Nov 22, 2003
    #2
    1. Advertising

  3. "Randell D." <> writes:

    > Basically, I want to write a javascript wherby I only need to pass it the
    > names of form fields - then my javascript will check each form field has a
    > value greater than zero in length - Any field value that fails this test is
    > noted... when all fields have been tested, an alert box opens telling the
    > user that they forgot to fill in 'x' number of fields and the field names
    > are named as they would appear visably on the form.


    Ok, that should be fairly simple.

    > I can write most of the script but there is a method that I would
    > like to do but don't know how to implement it in Javascript...
    >
    > A brief example of a form field would be the following HTML:
    >
    > Street Name: <input type=text size=30 name=streetName>


    > My solution to alert the user in a user friendly manner would be to have an
    > array with string indices. Thus the following piece of javascript:
    >
    > var requiredfields=new Array();
    > requiredfields['streetName']="Street Name";


    Use "new Object()". The only difference between objects and arrays is
    related to integer-indices. If you don't use such, you don't need
    an array.

    > The reason why I have it setup like this is I can use the index of an
    > element to check a specific field in a form. If that field string length is
    > zero, then the alert box will report "Street Name" and not "streetName"...
    > My question is: How can I create a loop that will give me the element name?


    > In PHP I could do something like :
    > foreach($requiredFields as $inputTagName=>$realFieldName)
    > { print("<br>Input tag $inputTagName is known by the user as
    > $realFieldName"); }


    In Javascript, the equivalent code would be:
    ---
    for (inputTagName in requiredFields) {
    document.write("<br>Input tag " + inputTagName +
    " is known by the user as " +
    requiredFields[inputTagName]);
    }
    ---
    (works the same whether you use Array or Object)

    > How can I achieve this in Javascript? How can I loop thru a string indexed
    > array? It does not have to be a for loop... but I do want to have the real
    > value of the array element, and the array element value...


    The construction:
    for (index in object) {...}
    will let the variable "index" iterate through the properties of the
    object "object". You can then access the property value as "object[index]".

    For this purpose, arrays are just objects.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 22, 2003
    #3
  4. "Lasse Reichstein Nielsen" <> wrote:

    > The only difference between objects and arrays is related to

    integer-indices.

    And length property, isn't that right?
     
    Vjekoslav Begovic, Nov 22, 2003
    #4
  5. "Vjekoslav Begovic" <> writes:

    > "Lasse Reichstein Nielsen" <> wrote:
    >
    >> The only difference between objects and arrays is related to

    > integer-indices.


    Your news-client isn't quoting correctly!

    > And length property, isn't that right?


    Yes, but that is also very much *related* to integer indices.

    More precisely:

    Arrays are objects with extra functionality.

    The Array.prototype object contains a number of methods that apply to
    objects with a numeric length property and properties with integer
    indices. Examples: push, join, concat. None of these require the object
    to be an array.

    The length property is "magic" (not something that can be programmed in
    Javascript itself). It is always at least one larger than the largest
    integer property of the object. Setting a larger integer named property
    increases the length. Setting the length lower removes properties.

    All in all, it is only about integer named properties. Even the length
    property is linked to these.

    This code shows how arrays work:
    ---
    function arrayInfo(arr) {
    var indices = [];
    var maxIndex = -1;
    for (var i in arr) {
    if (i > maxIndex) {maxIndex = i;}
    indices.push(i);
    }
    return "Length: " + arr.length +
    " Maximal index: " + maxIndex +
    " Indicies: {" + indices.join(",") + "}";
    }

    var x = [];
    alert(arrayInfo(x)); // length:0, maxIndex: -1, indicies {}
    x.length = 20;
    alert(arrayInfo(x)); // length:20, maxIndex: -1, indicies {}
    x[25] = 42;
    alert(arrayInfo(x)); // length:26, maxIndex: 25, indicies {}
    delete x[25];
    alert(arrayInfo(x)); // length:26, maxIndex: -1, indicies {}
    ---

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 22, 2003
    #5
  6. Randell D.

    Randell D. Guest

    "Eric Bohlman" <> wrote in message
    news:Xns943BAA7906E78ebohlmanomsdevcom@130.133.1.4...
    > "Randell D." <> wrote in
    > news:ypQvb.462767$6C4.34453@pd7tw1no:
    >
    > > My solution to alert the user in a user friendly manner would be to
    > > have an array with string indices. Thus the following piece of
    > > javascript:
    > >
    > > var requiredfields=new Array();
    > > requiredfields['streetName']="Street Name";

    >
    > In Javascript, "associative arrays" are implemented by Object rather than
    > Array. So make that first line:
    >
    > var requiredfields=new Object();
    >


    Me thinks the two books I do have are too old (or not written clear
    enough)... I have read, and understand a bit on javascript objects - its
    something I thought I understood but I guess I'm wrong. A good javascript
    book will be on my Christmas list this year I think...
     
    Randell D., Nov 23, 2003
    #6
  7. Randell D.

    Randell D. Guest

    "Randell D." <> wrote in message
    news:ypQvb.462767$6C4.34453@pd7tw1no...
    >
    > Folks,
    >
    > I can program fairly comfortably in PHP and can, for the most part using
    > these skills and others that I've picked up over the years manage to
    > read/understand most code in Javascript... so I'm just asking for a few
    > pointers (or the full solution if you have the time) for what I want to

    do.
    >
    > Basically, I want to write a javascript wherby I only need to pass it the
    > names of form fields - then my javascript will check each form field has a
    > value greater than zero in length - Any field value that fails this test

    is
    > noted... when all fields have been tested, an alert box opens telling the
    > user that they forgot to fill in 'x' number of fields and the field names
    > are named as they would appear visably on the form. I can write most of

    the
    > script but there is a method that I would like to do but don't know how to
    > implement it in Javascript...
    >
    > A brief example of a form field would be the following HTML:
    >
    > Street Name: <input type=text size=30 name=streetName>
    >
    >
    > My solution to alert the user in a user friendly manner would be to have

    an
    > array with string indices. Thus the following piece of javascript:
    >
    > var requiredfields=new Array();
    > requiredfields['streetName']="Street Name";
    >
    >
    > The reason why I have it setup like this is I can use the index of an
    > element to check a specific field in a form. If that field string length

    is
    > zero, then the alert box will report "Street Name" and not "streetName"...
    > My question is: How can I create a loop that will give me the element

    name?
    > In PHP I could do something like :
    >
    > foreach($requiredFields as $inputTagName=>$realFieldName)
    > { print("<br>Input tag $inputTagName is known by the user as
    > $realFieldName"); }
    >
    > How can I achieve this in Javascript? How can I loop thru a string

    indexed
    > array? It does not have to be a for loop... but I do want to have the real
    > value of the array element, and the array element value...
    >
    > Can anybody help me out? I'm still playing with the rest of my scrpit -

    I'm
    > confident that I can do the rest...
    >
    > Thanks in advance, replies please via the newsgroup... it might help

    someone
    > else...
    > randell d.
    >
    >


    Thanks to all who replied / I'll have to invest more time in my javascript -
    my books are at least two years old so that might be where I'm getting my
    knickers in a twist...

    I'll play a little more...

    Thanks again,
    randelld
     
    Randell D., Nov 23, 2003
    #7
  8. "Randell D." <> writes:

    > Me thinks the two books I do have are too old (or not written clear
    > enough)...


    Very likely :)

    > I have read, and understand a bit on javascript objects - its
    > something I thought I understood but I guess I'm wrong.


    Douglas Crockford has a good summary here:
    <URL:http://www.crockford.com/javascript/survey.html>

    > A good javascript book will be on my Christmas list this year I
    > think...


    I never had a good Javascript book myself, but the FAQ does recommend
    one. <URL:http://jibbering.com/faq/#FAQ3_1>

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 23, 2003
    #8
  9. JRS: In article <ypQvb.462767$6C4.34453@pd7tw1no>, seen in
    news:comp.lang.javascript, Randell D. <
    ..com> posted at Sat, 22 Nov 2003 21:19:26 :-

    >Basically, I want to write a javascript wherby I only need to pass it the
    >names of form fields - then my javascript will check each form field has a
    >value greater than zero in length - Any field value that fails this test is
    >noted... when all fields have been tested, an alert box opens telling the
    >user that they forgot to fill in 'x' number of fields and the field names
    >are named as they would appear visably on the form.


    That is similar to a subset of a situation discussed here recently; see

    Subject: Re: Phone Validation Problem (I reformatted the code to make
    it easier to read)
    Date: Fri, 21 Nov 2003 22:11:14 +0000
    Message-ID: <KzlOAzIC2ov$Ew$>

    The code given there is now obsolete; the current version (embodying
    suggested improvements) is at <URL:http://www.merlyn.demon.co.uk/jt.htm>.
    Since all your fields are to be tested similarly, you could possibly
    simplify the code a little, for instance by supplying a (constant)
    variable instead of unchanging literals.

    The question of seeking all that is to be tested has, I think, been
    adequately answered.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Nov 23, 2003
    #9
  10. > Me thinks the two books I do have are too old (or not written clear
    > enough)... I have read, and understand a bit on javascript objects - its
    > something I thought I understood but I guess I'm wrong. A good javascript
    > book will be on my Christmas list this year I think...


    Nearly all JavaScript books are horribly awful. Get a good one: Flanagan's
    Definitive Guide, 4th edition, from O'Reilly. Don't wait for Christmas.

    http://www.crockford.com/javascript/javascript.html
     
    Douglas Crockford, Nov 23, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin
    Replies:
    1
    Views:
    468
    Gunnar Hjalmarsson
    Jun 3, 2004
  2. b83503104
    Replies:
    3
    Views:
    3,913
    Al Bowers
    May 21, 2004
  3. Thomas Hawtin

    64-bit array indices RFE

    Thomas Hawtin, Aug 12, 2006, in forum: Java
    Replies:
    2
    Views:
    1,316
    Patricia Shanahan
    Aug 12, 2006
  4. KLaus Blank
    Replies:
    1
    Views:
    451
  5. Thomas Mlynarczyk

    array.length and string indices

    Thomas Mlynarczyk, Feb 15, 2004, in forum: Javascript
    Replies:
    7
    Views:
    93
    Thomas 'PointedEars' Lahn
    Feb 27, 2004
Loading...

Share This Page