chromedude
chromedude

Reputation: 4302

How do I make this loop through an array?

I have this jquery code:

function(returnArray){
     for (i=0; i<returnArray.length; i++) {
         $('<li class="tagSuggestTag"/>').appendTo('#tagSuggest ul').text(returnArray[i]);
     }

return array is an array, but for some reason when I do this it loops through every letter of the array instead of each value in the array.

The returnArray is ["hello", "helloe", "helloer"] and that loop goes through and returns:

enter image description here

Upvotes: 0

Views: 111

Answers (2)

Anders Lindahl
Anders Lindahl

Reputation: 42870

It was revealed in the comments to the question that the returnarray isn't really an array, it's a JSON string representation of a string computed by the PHP function json_encode().

The function jQuery.parseJSON can turn this back into a javascript array.

Upvotes: 2

herostwist
herostwist

Reputation: 3958

your array is a string. use:

var myarray = eval('["hello", "helloe", "helloer"]');

Upvotes: 1

Related Questions