vpaterno vpaterno - 8 months ago 17
Javascript Question

$.each is iterating through each character in array

I'm creating an array by pulling data from a json ajax request. But using that array in another

statement is resulting in iteration over each character rather than each value.

var setInfo = [];

url: blah,
async: false,
dataType: 'json',
success: function{
$.each(data.photosets.photoset, function(i,photoinfo) {
}) // completed set values look like 1234,5678,etc..

function holler() {
$.each(setInfo, function(index, value) {
// using each array value in here splits into each
// character rather than each value. So rather than
// iterating through each whole value 1234,5678,etc..
// It is iterating as 1,2,3,4,5,6,7,8,and so on.
var url1 = "http://www.test.com/" + value;
// Rather than http://www.test.com/1234, value is being evaluated as just 1
// so the result is http://www.test.com/1

Here's a sample of the json response in which I am getting the id:

[{"id":"123456789", "primary":"102932423", "secret":"19ca84349a", "server":"5143", "farm":6, "photos":"52", "videos":0, "title":{"_content":"Thanksgiving 2010"}, "description":{"_content":""}},
{"id":"012345678", "primary":"1294872352", "secret":"983a9c58d1", "server":"5184", "farm":6, "photos":"12", "videos":0, "title":{"_content":"24th Birthday Dinner at McCormick and Schmitts"}, "description":{"_content":""}}]}, "stat":"ok"})


It appears that I has a piece of code which I'm assuming was turning the array into a string:

setInfo += '';

running my code in console on firebug while on stackoverflow, I'm able to see that an array is now showing up as [1234,5678] BUT running locally, I'm getting a weird jQuery error:

uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMDocumentFragment.appendChild]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://code.jquery.com/jquery-1.5.js :: <TOP_LEVEL> :: line 5579" data: no]

Line 0


I tried the code and it works as expected with the data you provided. I would suggest to use console.log inside your function to see what's happening in your arrays.

> x=    ({"photosets":{"photo set":[{"id":"123456789", "primary":"102932423",...
> xx=[]
> $.each(x.photosets.photoset,function(i,photoinfo){ xx.push(photoinfo.id); });
> xx
["123456789", "012345678"]
> $.each(xx,function(index,value){ console.log(value); });