views:

4528

answers:

2

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!

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

each

Tim Büthe
Thanks, I didn't realise .each did that too!
tags2k
+6  A: 

You can use each for objects too and not just for arrays:

var obj = {
    foo: "bar",
    baz: "quux"
};
jQuery.each(obj, function(name, value) {
    alert(name + ": " + value);
});
Gumbo