0

The source is an excel file. I parsed and looped through the columns assigning headers. So far, this works fine. However I can't seem to access this data outside of the for loop as it comes up as "undefined" every time.

I am using the xls package and this is in javascript.

Changed the variable names to post the code due to sort of sensitive info. Let me know if there's any way I can clarify better. Thanks!

let data = []
for (let fname of fileList) {
        if (file_count > 1) {
            continue
        } else {
        }
        let excelFile = 
        XLSX.readFile(`FILE.xlsm`, 
        {
            cellDates: true,
        });
        log('FILENAME', fname)
        let sheet_name;
        for (let sheet in excelFile.Sheets) {
            sheet_name = sheet
            log('SHEET NAME', sheet)
        }
        let array = XLSX.utils.sheet_to_json(excelFile.Sheets[sheet_name], 
            {
                header: 
                ['HEADER_1', 
                'HEADER_2', 
                'HEADER_3']
            } );
        log('array', array[0].HEADER_1)
     
        for(let i = 0; i<array.length; i++){
            let data = {
                Obj_1: array[i].HEADER_1,
                Obj_2: array[i].HEADER_2,
                Obj_3: [i].HEADER_3
            }
            
        }
    }
  • `let data` defines a block scope variable, that does not exists outside of the loop. So why do you expect that you can access it outside of the loop? – t.niese Jun 11 '21 at 18:45
  • Apologies! I did declare "let data = []" as an empty array outside of the loop here: let data = [] for (let fname of fileList) { if (file_count > 1) { continue } else { } let excelFile = – user16201107 Jun 11 '21 at 18:47
  • But you have a `let data ` in the loop. Which is an entirely different variable (even if it has the same name). And that `let data ` in the loop only exists within the loop. – t.niese Jun 11 '21 at 18:47
  • Is there I way I can use .push maybe to use it outside? – user16201107 Jun 11 '21 at 18:50
  • Your code does not add anything to the array you have in `let data = []`. Furthermore, it would not be `undefined` but an empty array in the case of the shown code. In the current from your claim,`it comes up as "undefined" every time.` contradicts with the shown code. Create a [mcve] that where the code matches your problem description. – t.niese Jun 11 '21 at 18:50
  • Correct when I log "data" it comes out as [] but when I use one of the elements or typeof, it comes out as undefined. My issue is I can't access it outside of the loop. – user16201107 Jun 11 '21 at 18:55
  • `Is there I way I can use .push maybe to use it outside? ` yes by either renaming `let data = { Obj_1: … }` to something like `let item = { Obj_1: … }` and doing a `data.push(item)`, but you don't need a variable at al and could just do `data.push( { Obj_1: … })` – t.niese Jun 11 '21 at 19:02
  • I did this: ` const data =[]; for(let i = 0; i – user16201107 Jun 11 '21 at 19:29
  • As long as you don’t provide a [mcve] that shows where you try to access the data and where it is undefined. It is not possible to answer. For the code shown in your question only replacing `let data = { Obj_1: … }` with `data.push( { Obj_1: … })` has to work. – t.niese Jun 11 '21 at 19:34

1 Answers1

0

Variables which you define with let within the loop are block scoped. This means, your variable data is only accessible within the for loop.

What you can do to overcome this problem is, to use an array which you define outside of the loop.

For example

    for(let i = 0; i<array.length; i++){
        
         data.push({
             Obj_1: array[i].HEADER_1,
             Obj_2: array[i].HEADER_2,
             Obj_3: [i].HEADER_3
          });
    }

Then the data variable will be accessible outside of the loop and it will contain all of the other data.

The problem why your let data which you have used doesn't work is, because you declared the variable data again within the loop.

r4pt0s
  • 79
  • 10
  • Thanks! I applied this to my code. There are two loops here though. One for the files and another one for the data in the excel. I am not sure if I should keep both loops in or if I should leave data initialized outside of both loops or just outside the second loop. – user16201107 Jun 11 '21 at 19:08
  • Forgot to tag @r4pt0s – user16201107 Jun 11 '21 at 19:31
  • Of course, if you have two loops and you need both loops for processing the data, then you should leave let data=[] there where it is in your code. Just remove the let keyword within the loop and add the push method to push the data into the variable data. I'll update my answer. @user16201107 – r4pt0s Jun 11 '21 at 19:34