mardi 29 décembre 2020

FOR loop inside IF loop can't break out of IF after FOR loop if finished

I'm new at programming with Javascript & Jquery, and I'm creating a table of parent rows and child rows using DIVs, instead of a table. This is what the client wants. I need to find a way to exit the IF loop after one full iteration of the inner FOR loop with variable z. There are variabl number of instances where stringJSONArray.data[i].list > 1. The inner FOR loop creates a varying number of DIVs for each instance where stringJSONArray.data[i].list > 1. I need to exit the IF loop and return to the main FOR loop with variable i each time the FOR loop with variable z completes. Currently, the inner FOR loop with variable z is iterating through all instances in one go without breaking out of the IF loop and returning to the main FOR loop with variable i. This creates all the child rows below the table, instead of each group of child rows under its specific parent. Everything is working fine, except I can't break out of the IF loop each time the FOR loop iterates through the child row data. The condition needs to be determined when a row has a child, thus the IF loop. I can't find any other loop statement to use for this. Any help will be greatly appreciated.

var staticUrl = 'https://example.com/get-json-data'
jQuery.getJSON(staticUrl,function(result){
var jsonString = JSON.stringify(result)
const stringJSONArray = JSON.parse(jsonString);

var i;
for (i = 0; i < jsonString.length; i++) {

// Code to create parent rows


if (stringJSONArray.data[i].list > 1) {

var divElementSymbol = document.createTextNode(stringJSONArray.data[i].symbol);
var staticUrl3 = `https://example.com/get-json-child?symbol=${divElementSymbol.data}`;

jQuery.getJSON(staticUrl3,function(result3){
var jsonString3 = JSON.stringify(result3)
const stringJSONArray3 = JSON.parse(jsonString3);

let childCount = stringJSONArray.data[i].list;

var z;
for (z = 0; z < childCount; z++) {

let ChildElement1 = document.createElement('div');
let ChildElementText1 = document.createTextNode(stringJSONArray3[z].symbol);  
ChildElement1.appendChild(ChildElementText1);
let parentList1 = document.querySelector('#stock-id2');
parentList1.appendChild(ChildElement1);

}; //End (var z) Iterate Through All Child Rows

}); //End (STRINGIFY CHILD) Pull All Child Row Entries From get-json-child JSON Data Link

}; //End IF Loop To Second Find All Entries With Child Rows


}; // First loop i

}); //Close jQuery.getJSON function

Aucun commentaire:

Enregistrer un commentaire