Scope of global array javascript

Catherine

I am trying to load data from a csv and store it in an array of objects. I know global variables are frowned upon but I can't think of a better way to store the data and access it from multiple functions.

Here is my code:

var mydata = new Array;
$(document).ready( function () {
    $.get('./datafile.csv', function(data) {
        var head = data.split("\n");
        for(var i = 1; i < head.length; i++){
            line = head[i].split(",");
            var obj = {
                index:i,
                img:line[0],
                caption:line[1],
                desc:line[2]
            };
            mydata.push(obj);
        }
        console.log(mydata); //1
    });
    console.log(mydata); //2
    //I then want to select various elements on my page and set some attributes to
    //an object in my data, but I can't since everything is undefined
});

At the first spot it logs my data correctly, but at second spot it logs an empty array. I read this article on global variables in JavaScript so I'm not sure what is going wrong.

xs0

The second part (//2) runs too soon. When $.get executes, it just starts the HTTP request to get the CSV, but doesn't wait for it to finish - that's why you need to pass that function(data) in. After the request finishes, the callback function gets called, and it's there that you should continue your initialization.

So, your code should look something like that below. (if you need to use the data elsewhere, you can keep on using the global, but it's not needed just for this)

$(document).ready( function () {
    $.get('./datafile.csv', function(data) {
        var mydata = [];
        var head = data.split("\n");
        // ...

        console.log(mydata); //1
        continueSetup(mydata); // 2
    });
});

function continueSetup(mydata) {
    // do what you need
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

IIFE and Global scope in javascript

JavaScript scope issue with global variable

How to define an array with global scope in SilverStripe?

Global scope for array of structs inside function - Swift

How to specify const array in global scope in Rust?

Declare dynamically sized array in global scope

C++ Struct of array global scope

Javascript global array declaration

Are JavaScript functions in different <script>s in global scope?

Find and call javascript functions in global scope

Where are constants created in a global scope stored in JavaScript?

Scope chain in Javascript and calling nested function within the global scope

Javascript - Global scope or local scope, not able to figure it out

javascript global array cannot be logged

Set value of global array in javascript

Making an Array Variable Global in JavaScript

How to move all array elements up to global scope in PHP?

Push numbers to Array on click without using global Scope

How can an array of structs be initialized using a const struct in global scope?

Why javascript doesn't look up global scope in second case

Allow global scope to let variable from imported Javascript file

Javascript: Creating global scope functions from inside an class

Appcelerator / Titanium - Are Javascript variables polluting the global scope in controller files?

Isolating External Javascript from defining methods in the global (window) scope

In JavaScript; does a let variable declared outside of a block get a global scope?

Calling a method of a function constructor from another function in the global scope - Javascript

Javascript: While the variable is declared in global scope, it remains undefined inside the function

Changing Scope from Global to Local Breaking Javascript Program

JavaScript class definition local scope enable global code completion with tern