I have a JSON feed that is pulling into an each statement. What I would like to do is the for loop var i I would like this value to be used in the each statement. In the example below the for loop will run 0, 1, 2. If i == 1 then I would like to run the JSON feed but only for the i = 1 value. Currently when the if statement gets triggered the JSON each statement runs through all the items b/c it’s i value starts at 0. So the console.log I have in place returns 0, 1, 2. I would like it to return only 1. Thank you for any help.
EDIT
My focus is to make $.each(data.DATA, function(i, item) { the i within this each statement to use the number that i is equal to in the for loop statement. So if there are 3 items in the JSON file, only item 1 will be ran.
for (var i = 0; i < 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 to introduce an additional function scope to make this work.
Here, the callback passed to
$.getJSONis created inside another function. By doing that, the variable “i” that’s examined in the.each()loop is a copy of the “i” from theforloop. The handler function therefore has a fixed value to compare against.