Search code examples
javascriptlivecycle-designer

FOR loop and Array issue


I'm using Adobe Livecycle Designer ES4 to create some report. Based on XML a try to fill table. I have problem with Array. I push data into array in for loop. Below examples of my code:

  1. Results - blank textbox

    var print_data = xfa.record.containerPrintingData;
    var sfcArray = [];
    
    for (var i = 0; i < 10; i++) {
        sfc = print_data.resolveNode("sfcPrintingData["+ i +"]").sfc.value;    
        sfcArray.push(sfc);  
    };
    
    this.rawValue = sfcArray.toString();

  2. Results - get all items

    var print_data = xfa.record.containerPrintingData;
    var sfcArray = [];
    
    for (var i = 0; i < 10; i++) {
        sfc = print_data.resolveNode("sfcPrintingData["+ i +"]").sfc.value;    
        sfcArray.push(sfc);
        this.rawValue = sfcArray.toString();
    }

  3. Results - get 2nd item x 10

    var print_data = xfa.record.containerPrintingData;
    var sfcArray = [];
    
    for (var i = 0; i < 10; i++) {
        sfc = print_data.resolveNode("sfcPrintingData[1]").sfc.value;    
        sfcArray.push(sfc);
        this.rawValue = sfcArray.toString();
    }

Why 1st example don't work and 2nd work correct? I need use this array in another loops. How to solve it?


Solution

  • Because, If it has 2 items, and you looping it for 10.

    What happends is, when this.rawValue = sfcArray.toString(); is inside the loop, this.rawValue gets updated 2 times. First time One item will be there. second time 2 items. For the next iteration there is no 3rd item. So code breaks with error. But this.rawValue still have 2 items.

    Where as, when this.rawValue = sfcArray.toString(); is outside the loop, the code breaks with error and this.rawValue don't have any items in it.