javascript - Two types of for loop - how come only one of them is working? -
this question has answer here:
- why using “for…in” array iteration bad idea? 22 answers
i have following code in javascript:
var controller = { initapp: function(){ var getcartdata = $.ajax({ url: '/cart' }), getcategorydata = $.ajax({ url: '/categories' }), getsupplierdata = $.ajax({ url: '/suppliers'}), getprodcuts = $.ajax({url: '/products'}); // main controller logic starts when data loaded in $.when(getcartdata, getcategorydata, getsupplierdata, getprodcuts ).done( function( cart, categories, suppliers, products ) { var model = new model(cart, categories, suppliers, json.parse(products[0])); console.log(model.products); console.log(typeof model.products); for(var = 0; i<model.products.length; i++){ console.log(model.products[i]); } for(product in model.products){ console.log(product); } }); }
};
i iterate through model.products. @ end of code use 2 types of loops. how come 1 of them (the first one) working expected? second 1 prints numbers in length of array.
produkt key too:
a={a:1,b:2} for(key in a){ alert(key);// a. b. }
so need do:
for(product in model.products){ console.log(model[product]); }
but should use normal loop. why? because prototype handled in keys:
array.prototype.test=2;
run booth loops code executed before. in echo 2, dont want...
Comments
Post a Comment