looping through json array loops through the characters instead ofthe values

A

Aaron

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
 
L

Lasse Reichstein Nielsen

Aaron said:
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
 
D

dhtml

Aaron said:
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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top