javascript - Pass in For Loop Parameter into Each Statement -


i have json feed pulling each statement. loop var i value used in each statement. in example below loop run 0, 1, 2. if i == 1 run json feed i = 1 value. when if statement gets triggered json each statement runs through items b/c it's i value starts @ 0. console.log have in place returns 0, 1, 2. return 1. thank help.

edit
focus make $.each(data.data, function(i, item) { i within each statement use number i equal in loop statement. if there 3 items in json file, item 1 ran.

for (var = 0; < 2; i++) {     if (i == 1) {         $.getjson("/json/v2_ilt_map.cfm?feedtype=iltmap_scheduled&customerid=1&trainingobjectparent=3", {}, function(data, i) {             $.each(data.data, function(i, item) {                 console.log(i);             }         }):     } }​ 

you'll need introduce additional function scope make work.

function checkonly(i) {   return function(data) {     $.each(data.data, function(ii, item) {       if (ii == i) console.log(ii);     });   }; }  (var = 0; < 2; i++) {     if (i == 1) {         $.getjson("/json/v2_ilt_map.cfm?feedtype=iltmap_scheduled&customerid=1&trainingobjectparent=3", {}, checkonly(i));     } } 

here, callback passed $.getjson created inside function. doing that, variable "i" that's examined in .each() loop copy of "i" for loop. handler function therefore has fixed value compare against.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -