$.each() not properly iterating over array?

Your object has the property length which $. Each uses to iterate over the array. Since you are defining it, you are screwing up the behavior of the method.

Try changing the name to something else like length var export_pkg = { height : '10', _length : '3', depth : '30', weight : '40' }.

Your object has the property length which $. Each uses to iterate over the array. Since you are defining it, you are screwing up the behavior of the method.

Try changing the name to something else like _length var export_pkg = { height : '10', _length : '3', depth : '30', weight : '40' }.

AHHHHH that makes total sense now! I'm overwriting one of the built in properties! Thank you so very much!

– DirtyBirdNJ May 13 at 14:11 2 @DirtyBirdNJ: Nice, but just what I said 3 minutes before and with a working Fiddle to you... – ErickPetru May 13 at 14:17.

First, you isn't using an array, it's an object. But you can iterate object properties using each as you want. The length property is used by each to see how many items need to be iterated.

Since you have a length inside your object, it confuses each. I suggest you to change that property name: jsfiddle.net/ErickPetru/fFDfU/1.

Yep, nail on the head sir... I was overwriting a built in property. Works perfectly now! – DirtyBirdNJ May 13 at 14:38 Ok.

Thanks by the upvote. – ErickPetru May 13 at 15:00.

What you have is not an array of objects but an object with properties. An array is defined like this: var array = { height : $('#cubeiq_hght').text(), length : $('#cubeiq_lnth').text(), depth : $('#cubeiq_wdth').text(), weight : $('#cubeiq_wght').text() }, { height : $('#cubeiq_hght').text(), length : $('#cubeiq_lnth').text(), depth : $('#cubeiq_wdth').text(), weight : $('#cubeiq_wght').text() }, { ... } ; then you can loop: $. Each(array, function() { alert(this.

Height + ' ' + this. Length + ...); }); And to achieve what you want to achieve (loop through the properties of an object) you could do this: for (var propertyName in export_pkg) { alert(propertyName + ' ' + export_pkgpropertyName); }.

1 The each can be used to loop object properties, like the OP wants. – user736619 May 13 at 14:10.

Related Questions