jQuery each() giving me two different results with nested objects -


i'm trying use code below , works way want alerting honda, toyota, , ford in second each(). first 1 outputs 0. why doing that?

var cars = {         honda : {0: "accord", 1: "prelude", 2: "civic"},         toyota: {0: "camry", 1: "corolla", 2: "brz"},         ford: {0: "mustang", 1: "focus"} }  $(cars).each(function(key, value)) {     alert(key); })  $.each(cars, function(key, value) {     alert(key); }) 

your first example outputs 0 because when $(cars), you're wrapping cars object in jquery object, object 1 element index 0.

also should note distinction between both "each" methods:

jquery.each() or $.each():

a generic iterator function, can used seamlessly iterate on both objects , arrays.

.each():

iterate on jquery object, executing function each matched element.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -