1

I'm trying to write an .each statement for objects in a JSON array.

$.ajax({
    url: url,
    dataType: 'json',
    success: function(data){
        $.each(data, function (key, value){
            ...do stuff...
        }
});

Problem is, it's only returning one result because it's only doing an .each for data when I need it to do an .each for the objects inside data. I'm sure it's as simple as .each(data.something ...) but I can't figure out what it is.

EDIT Per comment request, data looks like:

[["data1","data2","data3"],
 ["data1","data2","data3"],
 ["data1","data2","data3"]
]

EDIT2

Here is a JSFiddle of my working code. if I change teh .each function to just do an alert it works, but when I try to build divs, it only spits out one entry.

8
  • 5
    what does the data object look like? Can you debug or do an alert(JSON.stringify(data)); ? Commented Dec 19, 2012 at 17:15
  • So you want it to loop through: data1a,data2a,data3a,data1b,data2b...? Commented Dec 19, 2012 at 17:22
  • I want to loop through [ [stuff], [stuff], [stuff] ] if that makes sense. Commented Dec 19, 2012 at 17:30
  • 1
    It does not. As far as I can tell, you're already doing that. Commented Dec 19, 2012 at 17:30
  • I know, right?! It's driving me nuts. Commented Dec 19, 2012 at 17:42

5 Answers 5

1

I originally suggested using $.map() to create an array.

But the actual problem was that the OP was overwriting the html in each iteration of the loop. Editing my response to show the actual answer.

Answer: OP needed to change .html() to .append() in his .each() loop.

Sign up to request clarification or add additional context in comments.

3 Comments

A user-defined function. The map function takes an array and a function as its parameters.
So should I have a function fn() somewhere? I'm confused.
Answer was in the comments, but you gave it. Maybe alter response?
1

Sounds like you need a nested loop:

$.ajax({
    url: url,
    dataType: 'json',
    success: function(data){
        $.each(data, function (idx1, value1){
            $.each(value1, function (idx2, value2){
                //...do stuff...
            });
        });
    });

4 Comments

Tried this, didn't work. It's not the data that's not displaying, it's the iteration of the .each.
I'm sorry, that makes no sense to me. Please clarify what you want to loop through in what order.
So, the JOSN array starts with [, then the first object is declared inside that array with another [ -- This is the one I want .each to iterate. After the second dimension, comes the actual data which is "data1","data2","data3", those are the values that need to be printed. so, for each 2nd level object, print its objects/values.
That sounds like what I originally had. I'm sorry, I don't think I can be much more help.
1

The problem with your fiddle isn't the .each loop -- it's that you're using .html() to add code, which will replace everything inside the specified element with your new code.

Use .append() instead of .html() and you should be fine.

Comments

0

Use one more loop

$.each(data, function(key, value) {
    $.each(value, function(i, val) {
        console.log(val)
    })
})​

Check Fiddle

1 Comment

What is the code that you are using to build the div's ... Can you update that code as well
0

Probably it is not a JSON object, just do the following

$.each($.parseJSON(data), function (key, value){
  ... stuff...
}

depending on the object, you should do a Map:

// Therefore, convert it to a real array
var realArray = $.makeArray( data )

// Now it can be used reliably with $.map()
$.map( realArray, function(val, i) {
  // do something 
});

Edit - Test on fiddle

With your object we can check that it is a nested array, so you have to double loop on it:

var data = [["data1","data2","data3"],
 ["data1","data2","data3"],
 ["data1","data2","data3"]
];

$.each(data, function(key, item) {
    $.each(item, function(i, val) {
        alert(val);
    })
});

fiddle

Edit 2 - Code for you situation!

$.ajax({
    url: url,
    dataType: 'json',
    success: function(data){
        $.each(data, function(key, item) {
            $.each(item, function(i, val) {
                alert(val);
            })
        }
    }
});

Hope it helps!!

2 Comments

Why is that not JSON? I was encoded as JSON from PHP, AJAX call is set to dataType: 'json', Wouldn't AJAX run the error: if it wasn't JSON?
The problem is it iterates through each data1 or data2 item. I want it to parse the parent level of those items. [["data1","data2"], //<---for each of these ["data1","data2"]//<--and this one too ], so the `.each function would run two times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.