-2

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)
        })
      })
    }); 

}
8
  • 4
    $.ajax is asynchronous ... whatever needs doing, do it in success callback - oh, wait, you're doing this with 'async': false - your second function does $.each(obj ... with no indication at all what obj actually is Commented Aug 9, 2024 at 7:52
  • 2
    Remove the 'async': false, and move putCodeblocks(cFiles) into the success function
    – mplungjan
    Commented Aug 9, 2024 at 8:14
  • Both Jaromanda X and mplungjan make excellent remarks.. obj was an old code that i forgot to remove.. and i have moved the call to the function in the success part of function 1
    – Wyrm
    Commented Aug 9, 2024 at 8:43
  • 1
    What does console.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.
    – fdomn-m
    Commented Aug 9, 2024 at 9:04
  • 1
    console.log(files.length) gives me 2, there are two objects in that array.. but im not sure that i am doing that right.. perhaps i need to create an array with just the filenames and not the keys?
    – Wyrm
    Commented Aug 9, 2024 at 9:47

1 Answer 1

-1

I've put everything in a single function.

First I do a single iteration and creates an array with just the filenames. Then in the second iteration i collect the JSON elements from the actual file.

function getCodeFiles(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');
       let arrFilez = []
       $.getJSON('JSON/'+sectiontype+'.json', function(filez){
        $.each(filez, function(key,value) {
            arrFilez.push(value.file)
            });
            
        $.each(arrFilez, function(key, filename){            
            $.getJSON('BKND/Codes/'+filename, function(data){
                $.each(data, function(key, value){
                    //console.log(value.title);
                    var codeTtl = value.title
                    var codeDsc = value.desc
                    var codeLnk = 'files/'+value.link
                    $('#PS').append($(
                        '<div class="codeblock">'
                        +'<span class="codeheader"><div class="codetype ps">'+c+'</div>'+codeTtl+'</span>'
                        +'<span class="content smalldesc">'
                        +'<div class="code">'
                        +'<desc>'+codeDsc+'</desc>'
                        +'<button class="mini ui blue icon button" title="open code in new window" data-href="'+codeLnk+'" data-title="'+codeTtl+'" id="codebutton"><i class="code icon"></i></button>'
                        +'</div>'
                        +'</span>'
                        +'</div>'
                    ));
                });
            });
        }); 
    });    
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.