I have a specific problem. I have one JSON file that contains all JSON files in a folder. I am iterating through that in a function where i create an array, I take that array and send it to another function where i for each filename in array 1 Iterate through and get all the keys and values of that JSON.
Ex: function1 , Create array from JSON
this is the JSON
[
{"file":"filename1.json"},
{"file":"filename2.json"}
]
this is the function:
function getFiles(c,sectiontype){ //This function triggers BKND asp to get all the files that is in BKND/Codes/ folder, and push out a JSON file under JSON/Codes/ folder.
// c is the type of code ASP, PS, CMD
$.ajax('BKND/'+sectiontype+'.asp');
var arrFiles=[];
$.ajax({
'async': false, //Set to false so that script waits for JSON to load.
'url': 'JSON/'+sectiontype+'.json', //URL to where to pick up JSON
'dataType': "json", //datatype
'success': function (data) { //if it succeeds
cFiles = data
}
});
putCodeblocks(cFiles)
}
this is the second function where i take the array from first function and iterate through each file anc collect all keys and values and put create the codeblocks in HTML
function putCodeblocks(files){
let zfiles = files.toString();
console.log(zfiles)
$.each(obj, function(key,value) {
$.getJSON('BKND/Codes/'+value.file, function(data){
$.each(data, function(index, element) {
console.log(element.title)
})
})
});
}
The first function works. it creates a JS array object, and i have checked that it is transferred to second function... But in second function, i only get the first filename and then the $.each loop stops? why?- What am i missing here?
EDIT: i changed somethings. but get the same result..
function getFiles(c,sectiontype){ //This function triggers BKND asp to get all the files that is in BKND/Codes/ folder, and push out a JSON file under JSON/Codes/ folder.
// c is the type of code ASP, PS, CMD
$.ajax('BKND/'+sectiontype+'.asp');
var arrFiles=[];
$.ajax({
'async': true, //Set to false so that script waits for JSON to load.
'url': 'JSON/'+sectiontype+'.json', //URL to where to pick up JSON
'dataType': "json", //datatype
'success': function (data) { //if it succeeds
putCodeblocks(data)
}
});
}
function putCodeblocks(files){
console.log(files)
$.each(files, function(key,value) {
$.getJSON('BKND/Codes/'+value.file, function(data){
$.each(data, function(index, element) {
console.log(element.title)
})
})
});
}
$.ajax
is asynchronous ... whatever needs doing, do it insuccess
callback - oh, wait, you're doing this with'async': false
- your second function does$.each(obj
... with no indication at all whatobj
actually is'async': false,
and moveputCodeblocks(cFiles)
into the success functionconsole.log(files.length)
give you? If it's 56 (given your example) or just not the number of files (2 in your example) then you have a JSON string, not an object.