javascript iterate - Iterating a JavaScript object's properties using jQuery

2 Answers

$.each( { name: "John", lang: "JS" }, function(i, n){
    alert( "Name: " + i + ", Value: " + n );


key value

Is there a jQuery way to perform iteration over an object's members, such as in:

    for (var member in obj) {

I just don't like this for sticking out from amongst my lovely jQuery notation!

This method will walk through object properties and write them to the console with increasing indent:

function enumerate(o,s){

    //if s isn't defined, set it to an empty string
    s = typeof s !== 'undefined' ? s : "";

    //iterate across o, passing keys as k and values as v
    $.each(o, function(k,v){

        //if v has nested depth
        if(typeof v == "object"){

            //write the key to the console
            console.log(s+k+": ");

            //recursively call enumerate on the nested properties
            enumerate(v,s+"  ");

        } else {

            //log the key & value
            console.log(s+k+": "+String(v));

Just pass it the object you want to iterate through:

var response = $.ajax({
    url: myurl,
    dataType: "json"
   console.log("Returned values:");
.fail(function(){ console.log("request failed");});