Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > looping through json array loops through the characters instead ofthe values

Reply
Thread Tools

looping through json array loops through the characters instead ofthe values

 
 
Aaron
Guest
Posts: n/a
 
      04-09-2011
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[i] + '">' + result[i] + '</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
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      04-09-2011
Aaron <(E-Mail Removed)> 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[i] + '">' + result[i] + '</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[i] = new Option(result[i], result[i]);
}
}
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'

 
Reply With Quote
 
 
 
 
dhtml
Guest
Posts: n/a
 
      04-10-2011
On Apr 9, 3:22*am, Lasse Reichstein Nielsen <(E-Mail Removed)>
wrote:
> Aaron <(E-Mail Removed)> 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[i] + '">' + result[i]+ '</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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM



Advertisments