10

I have the following object:

var objectVar = {
    4 : { "key" : "key-name4", "item4" : {} },
    3 : { "key" : "key-name3", "item3" : {} }
}

I then try the following:

$(objectVar).each(function(index,record){
    console.log(record); // Loops Only Once and Logs Full Object
});

Can anyone help me with why the $.each(); function inst iterating through the sub objects within the main object?

Any help would be appreciated!

5 Answers 5

16

"Can anyone help me with why the $.each(); function inst iterating through the sub objects within the main object?"

To loop the sub objects, you need sub loops.

While using the each()[docs] method like you have will usually sometimes work (as it currently is), it is really meant for DOM elements.

Instead use the jQuery.each()[docs] method:

$.each( objectVar, function(index,record){
    console.log(record); 

      // start a loop on the current record in the iteration
    $.each( record, function( index2, sub_record ) {
        console.log( index2, sub_record );
    });
});

Now your looping will be extended to the first level of nested objects.

If you're not sure of the overall structure, and want to enumerate the entire depth, you'll need to test each value encountered to see if it should be enumerated.

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

3 Comments

Thank you for the great explanation! I know its outside the scope of the OP, but do you know if there is a way to get the each() function to parse in order of the numbered keys instead of the order the records appear inside the object?
@dSquared: No, there isn't really any way to do that with an object. The only way would be to define an Array that describes the order you want, then iterate the Array, and fetch the related item from the object. So if your object has keys 7,3,9,4, you could create an Array arr=[3,4,7,9] to define the order, then iterate the Array, and do objectVar[ arr[i] ]. But that gets a little complicated.
...to get sequential ordering of numeric keys, you should really use an Array in the first place.
5

You should use the $.each method instead of the .each method:

$.each(objectVar, function(index, record) {
  console.log(record);
});

Comments

3
$.each(objectVar,function(index,record){
    console.log(record);

});

Comments

3

You're using $.each() incorrectly for non-jQuery objects:

$.each( objectVar, function( index, record ){
    console.log( record );
});

jQuery.each()

Comments

3

While jQuery is great, you really aren't using it. JavaScript looping through objects is fairly simple as is:

var record;
for(var key in objectVar) {
    record = objectVar[key];
}

3 Comments

The key may be other values, like text, not necessarily the index for the actual items. Won't work.
@dspjm you can use text (strings) to access properties on a javascript object just like an array. They both work since it's all objects.
it's not only the problem of text. The key will be names not in the array, but of jQuery itself. For example, length, each, map, etc. Print the key and you will see. $.each() should be used instead.

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.