vote up 0 vote down
star

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!

flag
add comment

2 Answers

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

each

link|flag
Thanks, I didn't realise .each did that too! – tags2k Jul 8 at 9:46
add comment
vote up 2 vote down

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);
});
link|flag
add comment

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.