Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop through each JSON response returns "[Object Object]"

I have this on success AJAX function

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            alert(value);
        });
    }   
}

and this the JSON response from the console (refer to the image below)

JSON response from the console

but it throws me "[Object Object]" from the alert prompt, any ideas, clues, help, suggestions, recommendations?

like image 312
Juliver Galleto Avatar asked Jul 25 '26 21:07

Juliver Galleto


2 Answers

Do not use alert but console.log instead. You will be able to look into all the objects that way and avoid getting spammed.

Also, if you need to look into a deep object, you can use something like https://github.com/WebReflection/circular-json which will allow to print even objects that references themselves (circular reference would fail to print big object).

like image 183
Vadorequest Avatar answered Jul 27 '26 12:07

Vadorequest


alert uses the object's toString method, which will return this [Object Object] thing. If you want to print an object nicely, you can use JSON.stringify(yourObject)

like image 45
Yaron Schwimmer Avatar answered Jul 27 '26 12:07

Yaron Schwimmer