looping through json array loops through the characters instead ofthe values

Discussion in 'Javascript' started by Aaron, Apr 9, 2011.

  1. Aaron

    Aaron Guest

    Hi there,

    I'm attempting to loop through a very simple array of the form:

    ["colour", "second value"]

    I am using:

    for (var i = 0; i < result.length; i++) {
    options += '<option value="' + result + '">' + result + '</option>';
    }

    with the idea that options is written to a select box.

    An example page is at: http://test-psi-alpha.appspot.com/jquery3

    The problem is as follows:

    result seems to be treated as a string instead of an array because result.length gives the number of characters as if it were a string and the loop loops through each character.

    Any clues on how to solve this beast would be appreciated.

    Aaron
    Aaron, Apr 9, 2011
    #1
    1. Advertising

  2. Re: looping through json array loops through the characters insteadof the values

    Aaron <> writes:

    > Hi there,
    >
    > I'm attempting to loop through a very simple array of the form:
    >
    > ["colour", "second value"]
    >
    > I am using:
    >
    > for (var i = 0; i < result.length; i++) {
    > options += '<option value="' + result + '">' + result + '</option>';
    > }
    >
    > with the idea that options is written to a select box.
    >
    > An example page is at: http://test-psi-alpha.appspot.com/jquery3
    >
    > The problem is as follows:
    >
    > result seems to be treated as a string instead of an array because result.length gives the number of characters as if it were a string and the loop loops through each character.


    The result probably *is* a string.
    If you want to be absolutely sure, you can add (typeof result) to your alert.

    > Any clues on how to solve this beast would be appreciated.


    If result is a returned JSON string, then you need to parse it.
    I'm sure JQuery has some way to parse JSON, hopefully defaulting to calling
    JSON.parse in any modern browser.

    As for the code, I don't really see what using JQuery buys you here, except
    the XmlHTTPRequest interface.

    The body of function could just as well have been:
    function (result) {
    result = JSON.parse(result); // Make sure JSON.parse exists.
    var box2 = document.getElementById("box2");
    box2.options.length = 0;
    for (var i = 0; i < result.length; i++) {
    box2.options = new Option(result, result);
    }
    }
    Seems cleaner to me (but then, I'm a purist).

    In any case:
    Everywhere you write "select#box2", you can just write "#box2". The
    "select" is irrelevant since the id uniquely identifies the element.
    You clear the innerHTML of box2 twice. Once should be enough.
    The "var result = ''" line can be omitted. It does nothing.
    The "var options = ''" should be put into the function using it. No
    need to complicate the scope chain.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Apr 9, 2011
    #2
    1. Advertising

  3. Aaron

    dhtml Guest

    Re: looping through json array loops through the characters insteadof the values

    On Apr 9, 3:22 am, Lasse Reichstein Nielsen <>
    wrote:
    > Aaron <> writes:
    > > Hi there,

    >
    > > I'm attempting to loop through a very simple array of the form:

    >
    > > ["colour", "second value"]

    >
    > > I am using:

    >
    > > for (var i = 0; i < result.length; i++) {
    > >     options += '<option value="' + result + '">' + result+ '</option>';
    > >           }

    >
    > > with the idea that options is written to a select box.

    >
    > > An example page is at:http://test-psi-alpha.appspot.com/jquery3

    >
    > > The problem is as follows:

    >
    > > result seems to be treated as a string instead of an array because result.length gives the number of characters as if it were a string and the loop loops through each character.

    >
    > The result probably *is* a string.
    > If you want to be absolutely sure, you can add (typeof result) to your alert.
    >
    > > Any clues on how to solve this beast would be appreciated.

    >
    > If result is a returned JSON string, then you need to parse it.
    > I'm sure JQuery has some way to parse JSON, hopefully defaulting to calling
    > JSON.parse in any modern browser.
    >


    Sure != hopeful.

    Last I checked, jQuery used indirect eval. I'm not reading jQuery
    these days and not working either.
    --
    Garrett
    dhtml, Apr 10, 2011
    #3
    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. Florian Frank
    Replies:
    0
    Views:
    230
    Florian Frank
    Jun 30, 2009
  2. Replies:
    5
    Views:
    263
  3. sajuptpm
    Replies:
    2
    Views:
    316
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    257
    Bryan Britten
    May 28, 2013
Loading...

Share This Page